Skip to content

Commit 8d577d2

Browse files
bites 199 - Multiple inheritance
1 parent 2244903 commit 8d577d2

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@
7171
/148/README.md
7272
/197/README.md
7373
/222/README.md
74+
/199/README.md

199/person.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# see __mro__ output in Bite description
2+
class Person:
3+
def __init__(self, end='\n'):
4+
self.msg = "I am a person"
5+
6+
def __str__(self):
7+
return self.msg
8+
9+
10+
class Father(Person):
11+
def __init__(self, end=''):
12+
super(Father, self).__init__(end='')
13+
self.msg = self.msg + " and cool daddy"
14+
15+
def __str__(self):
16+
return self.msg
17+
18+
19+
class Mother(Person):
20+
def __init__(self, end=''):
21+
super(Mother, self).__init__(end='')
22+
self.msg = self.msg + " and awesome mom"
23+
24+
def __str__(self):
25+
return self.msg
26+
27+
28+
class Child(Father, Mother, Person):
29+
def __init__(self):
30+
super().__init__(end="")
31+
self.msg = "I am the coolest kid"
32+
33+
def __str__(self):
34+
return self.msg
35+
36+
37+
def main():
38+
person = Person()
39+
dad = Father()
40+
mom = Mother()
41+
child = Child()
42+
pass
43+
44+
if __name__ == "__main__":
45+
main()

199/test_person.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import inspect
2+
3+
import pytest
4+
5+
from person import Person, Father, Mother, Child
6+
7+
8+
@pytest.fixture
9+
def person():
10+
return Person()
11+
12+
13+
@pytest.fixture
14+
def dad():
15+
return Father()
16+
17+
18+
@pytest.fixture
19+
def mom():
20+
return Mother()
21+
22+
23+
@pytest.fixture
24+
def child():
25+
return Child()
26+
27+
28+
def test_string_repr_person(person):
29+
assert str(person) == 'I am a person'
30+
31+
32+
def test_string_repr_dad(dad):
33+
assert str(dad) == 'I am a person and cool daddy'
34+
35+
36+
def test_string_repr_mom(mom):
37+
assert str(mom) == 'I am a person and awesome mom'
38+
39+
40+
def test_string_repr_child(child):
41+
assert str(child) == 'I am the coolest kid'
42+
43+
44+
def test_mro_of_person():
45+
assert Person.__mro__ == (Person, object)
46+
47+
48+
def test_mro_of_dad():
49+
assert Father.__mro__ == (Father, Person, object)
50+
51+
52+
def test_mro_of_mom():
53+
assert Mother.__mro__ == (Mother, Person, object)
54+
55+
56+
def test_mro_of_child():
57+
assert Child.__mro__ == (Child, Father, Mother, Person, object)
58+
59+
60+
def test_subclass_person():
61+
assert issubclass(Person, object)
62+
63+
64+
def test_subclass_dad():
65+
assert issubclass(Father, Person)
66+
assert issubclass(Father, object)
67+
68+
69+
def test_subclass_mom():
70+
assert issubclass(Mother, Person)
71+
assert issubclass(Mother, object)
72+
73+
74+
def test_subclass_child():
75+
assert issubclass(Child, Father)
76+
assert issubclass(Child, Mother)
77+
assert issubclass(Child, Person)
78+
assert issubclass(Child, object)
79+
80+
81+
def test_use_inheritance():
82+
# dry code!
83+
# should not duplicate substr in subclass
84+
substr = 'I am a person'
85+
for src in (inspect.getsource(Father),
86+
inspect.getsource(Mother)):
87+
assert substr not in src

0 commit comments

Comments
 (0)