@@ -7,21 +7,21 @@ def fly(self):
77 if self .ratio > 1 :
88 print ("Weee, this is fun" )
99 elif self .ratio == 1 :
10- print ("This is hard word , but I'm flying" )
10+ print ("This is hard work , but I'm flying" )
1111 else :
1212 print ("I think I'll just walk" )
1313
1414
1515class Duck (object ):
1616
17- def __int__ (self ):
17+ def __init__ (self ):
1818 self ._wing = Wing (1.8 )
1919
2020 def walk (self ):
2121 print ("Waddle, waddle, waddle" )
2222
2323 def swim (self ):
24- print ("Come on it, th water's lovely" )
24+ print ("Come on in, the water's lovely" )
2525
2626 def quack (self ):
2727 print ("Quack quack" )
@@ -32,24 +32,60 @@ def fly(self):
3232
3333class Penguin (object ):
3434
35+ def __init__ (self ):
36+ self .fly = self .aviate
37+
3538 def walk (self ):
3639 print ("Waddle, waddle, I waddle too" )
3740
3841 def swim (self ):
39- print ("Come on in, it's a bit chilly this far South" )
42+ print ("Come on in, but it's a bit chilly this far South" )
4043
4144 def quack (self ):
42- print ("Are you 'avin' a larf? I'm penguin!" )
45+ print ("Are you 'avin' a larf? I'm a penguin!" )
46+
47+ def aviate (self ):
48+ print ("I won the lottery and bought a learjet" )
4349
4450
4551# def test_duck(duck):
4652# duck.walk()
4753# duck.swim()
4854# duck.quack()
4955
56+ class Mallard (Duck ):
57+ pass
58+
59+
60+ class Flock (object ):
61+
62+ def __init__ (self ):
63+ self .flock = []
64+
65+ def add_duck (self , duck : Duck ) -> None :
66+ fly_method = getattr (duck , 'fly' , None )
67+ # if isinstance(duck, Duck):
68+ if callable (fly_method ):
69+ self .flock .append (duck )
70+ else :
71+ raise TypeError ("Cannot add duck, are you sure it's not a " + str (type (duck ).__name__ ))
72+
73+ def migrate (self ):
74+ problem = None
75+ for duck in self .flock :
76+ try :
77+ duck .fly ()
78+ raise AttributeError ("Testing exception handler in migrate" ) # TODO remove before release
79+ except AttributeError as e :
80+ print ('One duck down' )
81+ problem = e
82+ if problem :
83+ raise problem
84+
5085
5186if __name__ == '__main__' :
5287 donald = Duck ()
88+ donald .fly ()
5389
5490 # percy = Penguin()
5591 # test_duck(percy)
0 commit comments