(Jul-04-2018, 10:36 PM)Truman Wrote: 1. why in class Pets it is necessary to open an empty listCan just delete has no function(yet) other than it's a empty list that belong to the class,the output will be same.
You can look at list at bye calling it with class name.
I have 3 dogs. Tom is 6 Fletcher is 7 Larry is 9 And they're all mammals, of course. # Call it >>> Pets.dogs [] # The instance attributes has the same name "dogs",but it don't overwrite the class attribute dogs >>> my_pets.dogs [<__main__.Bulldog object at 0x03B2AE30>, <__main__.RussellTerrier object at 0x03B2AE70>, <__main__.Dog object at 0x03B2ADF0>] >>>
Quote:2. this line confuses me:It just used to count,so in the list there will be 3 new instances.
Python Code: (Double-click to select all)
print(f'I have {len(my_pets.dogs)} dogs.')why my_pets.dogs? I don't see a connection. dogs is attribute of the class Pet. Does dogs represents names in my_dogs list methods?
>>> my_pets.dogs [<__main__.Bulldog object at 0x03B2AE70>, <__main__.RussellTerrier object at 0x03B2ADF0>, <__main__.Dog object at 0x03B2AD50>] >>> len(my_pets.dogs) 3 # In the loop this happens when methods get called >>> my_pets.dogs[0].name 'Tom' >>> my_pets.dogs[0].age 6
Quote:what is dog?See if output is the same
for dog in my_pets.dogs: print(f'{dog.name} is {dog.age}') # Try for rubberduck in my_pets.dogs: print(f'{rubberduck.name} is {rubberduck.age}')