Note 13.5.1.
Even though initializer methods create objects, they do not have a
return
statement in them.str
, int
, float
and Turtle
. These were defined by Python and made available for us to use. However, in many cases when we are solving problems, we need to create data objects that are related to the problem we are trying to solve. We need to create our own classes.Rectangle
object as shown here.area
or its perimeter
. You may also wish to calculate the length of the diagonal of the rectangle, or determine the ratio of the width to height. We’ll shortly see how we can organize these together with the data.Rectangle
object might look like, we can define a new class. We’ll want our rectangles to each have a width
and a height
attribute, so our first class definition looks like this.class Rectangle:
""" Rectangle class for representing and manipulating width and height. """
def __init__(self):
""" Create a new 1 by 1 rectangle """
self.width = 1
self.height = 1
import
statements). The syntax rules for a class definition are the same as for other compound statements. There is a header which begins with the keyword class
, followed by the name of the class, and ending with a colon.__init__
. This initializer method, often referred to as the constructor, is automatically called whenever a new instance of Rectangle
is created. It gives the programmer the opportunity to set up the attributes required within the new instance by giving them their initial state values. The self
parameter is automatically set to reference the newly-created object. You can think of self
as meaning “the object we are currently working with” (in this case, creating). There is nothing special about the parameter name self
, but it’s an unwritten rule that all Python programmers use that variable name in object constructors.return
statement in them.width
and height
, but to self.width
and self.height
. The attributes width
and height
are always attached to a particular instance. The instance is always explicitly referenced with dot notation.Rectangles
have been created, each having a width and height with value 1. However, because we have not asked the rectangles to do anything, we don’t see any other result.r1
and r2
are assigned references to two new Rectangle
objects. A function like Turtle
or Rectangle
that creates a new object instance is called a constructor. Every class automatically uses the name of the class as the name of the constructor function. Using the construtor implicity calls the __init__
method.r1
and r2
are Rectangle
objects. If you run this program with a Python interpreter, the output will look like this:Nothing seems to have happened with the rectangles r1 is <__main__.Rectangle object at 0x7f3048cd6320> r2 is <__main__.Rectangle object at 0x7f3048cd7880> Are r1 and r2 the same object? False
at
in the output shows the memory address for the object reference. You can see that r1
and r2
refer to different locations. Even though both r1
and r2
are instances of Rectangle
, they aren’t the same object, and the is
operator confirms this by returning False
.class Car:
def __init__(self):
self.color = "Red"
self.miles_per_gallon = 27
bmw = Car()
ford = Car()
test1 = bmw is ford
test2 = type(bmw) == type(ford)
print(test1, test2)
BMW
object is not the Ford
object, but they are of the same type.