classDog:
alive=0def __init__(self, color='white', breed='mutt'):
self.color=color
self.breed=breed
Dog.alive=Dog.alive+1def __del__(self):
Dog.alive -=1defspeak(self):
print('Wuf Wuf')
def __str__(self):
return (f'this dog is a {self.color} {self.breed}')
classPuppy(Dog):
def __init__(self, color='white', breed='mutt' ,wormed=True):
#choose one of the following 2 lines
Dog.__init__(self,color,breed)
#super().__init__(color,breed) #notice no self passed as first arg for super().
self.dewormed=wormed
defspeak(self): #if not defined puppies will speak Wuf Wuf as the parent Dogprint('ruf')
def __str__(self):
return (f'this Puppy is a {self.color} {self.breed}')
defbirth():
fido3=Dog('grey','shepard') #local var
fido4=Dog() #local varprint (f'dogs alive in func = {Dog.alive}')
#at the end of this func all local vars are garbage collected (they are deleted so the del func is called)
fido1=Dog() #init is called when a dog is created with default args of white and mutt
fido2=Dog('black', 'lab')
fido1.speak()
fido2.speak()
print(fido1)
print(fido2)
print('before func',Dog.alive)
birth()
print('after func',Dog.alive)
fido5=Dog()
print(Dog.alive)
pup1=Puppy()
pup2=Puppy('brown', 'pointer')
pup1.speak()
print(f'after puppies are born {Dog.alive}')
print(pup1)