Jul-06-2018, 10:00 PM
Here we go again:
# Parent class class Dog: # Class attribute species = 'mammal' # Initializer / Instance attributes def __init__(self, name, age): self.name = name self.age = age # instance method def description(self): return "{} is {} years old".format(self.name, self.age) # instance method def speak(self, sound): return "{} says {}".format(self.name, sound) # Child class (inherits from Dog class) class RussellTerrier(Dog): def run(self, speed): return "{} runs {}".format(self.name, speed) # Child class (inherits from Dog class) class Bulldog(Dog): def run(self, speed): return "{} runs {}".format(self.name, speed) # Parent class class Pets: dogs = [] def __init__(self, dogs): self.dogs = dogs my_dogs = [ Bulldog("Tom", 6), RussellTerrier("Fletcher", 7), Dog("Larry", 9) ] # Instantiate the Pets class my_pets = Pets(my_dogs) print(f'I have {len(my_pets.dogs)} dogs.') for dog in my_pets.dogs: print(f'{dog.name} is {dog.age}') print(f'And they\'re all {dog.species}s, of course.') print(Pets.dogs) print(my_pets.dogs)I just complicated without a reason, basically without dogs list I can't print Pets class and my_pets instance.