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

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Python object methods

Python object methods

- [Instructor] In the previous video, we discussed class properties or attributes, which are the data associated with objects from that class. Now we're going to discuss class methods, which are the behavior associated with objects made from that class. So we're going to add to our rectangle now a method which is going to give us the area. Now we could set this as another property simply by doing base time height, but what if we want to display it in a particular format. That's going to make more sense really to have as a method or a behavior of the class. So we're going to create this method and we have to have this self argument even if there are no other parameters. So now we're going to print and we're going to use an fstring, area of rectangle, and then use these curly braces to interpolate the value, which is going to be self.base, multiply by self.height, and that's going to be square units, right? So whether it's millimeters or inches or meters, whatever. Okay, so now we have…

Contents