From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach

Python object properties

- [Instructor] In this video, we're going to be looking at class properties and how to access and modify them. So we're going to start with a different example just for variety. And we're going to create a class. So code along with this. It's going to be a rectangle, okay? So geometrical shape of a rectangle. And in the constructor, we're going to have two parameters, although of course it's going to be this third mystical self parameter, which eventually will become less mystical. So we're going to have a base, and we're going to have height. Okay, so self.base. So this means the base of the particular instance of the particular object is going to be equal to that input parameter, and self.height is going to be equal to that input parameter height here. Okay, and that's it for our class definition for now. So let's create an instance of that. So we'll call it new rectangle. You can call it whatever you like, but that seems like a reasonable name. And it's going to be an instance of rectangle. Okay, so the capital R there shows that we're calling, implicitly calling this constructor in the class definition with the same name. And let's have some values, 10 and 12 maybe. And now we can print, and I use tab there to auto complete a new rectangle.base. And then that was shift + alt and down. A lot of this is just IDE specific, but it's quite useful to know. So shift + alt and down gives you a new and duplicate of the previous line, but we don't want height for this. Sorry, we don't want base, we want height. Okay, let's see what we've got when we press the run button. Now there is another way to run files. In the console, you could do Python, and then you can have the full Path to a specific file, and it would run it. But I find it much easier simply to use the the go button personally. So what have we done? We've created a template for rectangles, and we said that when we create them, we want to allocate a base value and a height value. Okay, and then we've gone ahead and created an instance. So that's an object of the class rectangle. And we've parked in these parameters, 12 and 10, and they got allocated via this constructor. And now we're accessing by simply using this dot notation. So we print new rectangle.base. So that means the base of or the base property or attributes. So property and attribute are often used interchangeably. So the base property of this new rectangle. So we use that dot notation, and the height property of this new rectangle. Okay, so far, so good. What about if we want to modify the base or the height of direct rectangle? Well, Python lets you do this directly by simply overriding object properties. So for example, if you want to do that, you can add these lines to the code. You can do new, rectangle, tab to get there. Auto complete there, dot okay, so we're simply taking that property and overwriting it. So now we can print new rectangle.base and let's just put in a print statement just to have a bit of breaking up of the output. So we had 12 and 10 from the previous output and then we had five from the new output. It's really that simple. You can directly overwrite the property of an object that you've created by using that dot notation and simply assigning a new value. I should mention that there are reasons why in future you might not want to access and update object properties directly as we have done here. But there's often a difference between what you might do while you're learning a topic and what you might do later on when you have a wider understanding of the context. So this is absolutely fine for now, and very often in the world you'll see properties being accessed and updated in this very direct way.

Contents