Creating instances of classes in Python

We already created instances of the simple Rectangle class. We just needed to use the class name, specify the required arguments enclosed in parentheses, and assign the result to a variable.

The following lines declare a calculate_area method within the body of the Rectangle class:

def calculate_area(self):
    return self.width * self.height

The method doesn't require arguments to be called from an instance because it just declares the previously explained self parameter. The following lines create an instance of the Rectangle class named rectangle4 and then print the results of the call to the calculate_area method for this object:

rectangle4 = Rectangle(143, 187)
print(rectangle4.calculate_area())

Now, imagine ...

Get JavaScript : Object-Oriented Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.