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.heightThe 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 that we want to have a function that receives the width and height values of a rectangle and returns the calculated area. We can take advantage of the Rectangle class to code this new function. We just need to create an instance of the Rectangle...