Skip to content

Commit 50c597c

Browse files
committed
No need to define __init__ in Wolf, Sheep, Snake, Parrot classes
also set number_of_legs in Animals class, and pass the number of legs in *LeggedAnimal call to super().__init__, that is to minmize changes from e43_animals
1 parent 4f956bd commit 50c597c

1 file changed

Lines changed: 5 additions & 22 deletions

File tree

ch09-objects/e43b1_legged_animals.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,38 @@
66
class Animal():
77
"""Base class for animals. Not meant to be instantiated."""
88

9-
def __init__(self, color):
9+
def __init__(self, color, number_of_legs):
1010
self.species = self.__class__.__name__
1111
self.color = color
12+
self.number_of_legs = number_of_legs
1213

1314
def __repr__(self):
1415
return f'{self.color} {self.species}, {self.number_of_legs} legs'
1516

1617

1718
class TwoLeggedAnimal(Animal):
1819
def __init__(self, color):
19-
super().__init__(color)
20-
self.number_of_legs = 2
20+
super().__init__(color, 2)
2121

2222

2323
class FourLeggedAnimal(Animal):
2424
def __init__(self, color):
25-
super().__init__(color)
26-
self.number_of_legs = 4
25+
super().__init__(color, 4)
2726

2827

2928
class ZeroLeggedAnimal(Animal):
3029
def __init__(self, color):
31-
super().__init__(color)
32-
self.number_of_legs = 0
30+
super().__init__(color, 0)
3331

3432

3533
class Wolf(FourLeggedAnimal):
3634
"""Class for creating 4-legged wolves of any color"""
3735

38-
def __init__(self, color):
39-
super().__init__(color)
40-
41-
4236
class Sheep(FourLeggedAnimal):
4337
"""Class for creating 4-legged sheep of any color"""
4438

45-
def __init__(self, color):
46-
super().__init__(color)
47-
48-
4939
class Snake(ZeroLeggedAnimal):
5040
"""Class for creating 0-legged snakes of any color"""
5141

52-
def __init__(self, color):
53-
super().__init__(color)
54-
55-
5642
class Parrot(TwoLeggedAnimal):
5743
"""Class for creating 2-legged parrots of any color"""
58-
59-
def __init__(self, color):
60-
super().__init__(color)

0 commit comments

Comments
 (0)