Inheritance

Lesson 11: Inheritance

class Dog:

    alive=0

    def __init__(self, color='white', breed='mutt'):
        self.color=color
        self.breed=breed
        Dog.alive=Dog.alive+1

    def __del__(self):
        Dog.alive -= 1


    def speak(self):
        print('Wuf Wuf')

    def __str__(self):
        return (f'this dog is a {self.color} {self.breed}')

class Puppy(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

    def speak(self): #if not defined puppies will speak Wuf Wuf as the parent Dog
        print('ruf')

    def __str__(self):
        return (f'this Puppy is a {self.color} {self.breed}')


def birth():
    fido3=Dog('grey','shepard') #local var
    fido4=Dog()                 #local var
    print (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)

class MyApp(Fl_Window):
       def __init__(self, w, h, label): #x,y,w,h,l     w,h,l
               Fl_Window.__init__(self, w, h, label)

               self.begin()
               self.but = Fl_Button(95, 5, 140, 50, "Not clicked yet")
               self.but.callback(self.but_cb)
               self.but.color(FL_BLUE)

               self.inp = Fl_Input(95, 90, 140, 50,"Input")  #x,y,w,h,label
               self.end()
               self.show()

       def but_cb(self, wid):
               wid.label(self.inp.value())  #wid is the self.but
               self.but.label(self.inp.value())  #wid is the self.but

if __name__=='__main__':
    Fl.scheme("plastic")
    app = MyApp(450, 155, "Widget communication")
    Fl.run()