Skip to main content

Section 16.9 Multiple instances

So far, we have defined a class, constructed a single object, used that object, and then thrown the object away. However, the real power in object-oriented programming happens when we construct multiple instances of our class.
When we construct multiple objects from our class, we might want to set up different initial values for each of the objects. We can pass data to the constructors to give each object a different initial value:
The constructor has both a self parameter that points to the object instance and additional parameters that are passed into the constructor as the object is constructed:
s = PartyAnimal('Sally')
Within the constructor, the second line copies the parameter (nam) that is passed into the name attribute within the object instance.
self.name = nam
The output of the program shows that each of the objects (s and j) contain their own independent copies of x and nam:
Sally constructed
Sally party count 1
Jim constructed
Jim party count 1
Sally party count 2

Checkpoint 16.9.1.

Q-2: When using multiple instances, we can set different initial values for the instances by using a .