FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_koans/python3/koans/about_inheritance.py at master · arksega/python_koans · GitHub
arksega
python_koans
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python_koans
/
python3
/
koans
/
about_inheritance.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
89 lines (64 loc) · 2.69 KB
Breadcrumbs
python_koans
/
python3
/
koans
/
about_inheritance.py
Copy path
File metadata and controls
89 lines (64 loc) · 2.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from
runner
.
koan
import
*
class
AboutInheritance
(
Koan
):
class
Dog
:
def
__init__
(
self
,
name
):
self
.
_name
=
name
@
property
def
name
(
self
):
return
self
.
_name
def
bark
(
self
):
return
"WOOF"
class
Chihuahua
(
Dog
):
def
wag
(
self
):
return
"happy"
def
bark
(
self
):
return
"yip"
def
test_subclasses_have_the_parent_as_an_ancestor
(
self
):
self
.
assertEqual
(
__
,
issubclass
(
self
.
Chihuahua
,
self
.
Dog
))
def
test_this_all_classes_in_python_3_ultimately_inherit_from_object_class
(
self
):
self
.
assertEqual
(
__
,
issubclass
(
self
.
Chihuahua
,
object
))
# Note: This isn't the case in Python 2. In that version you have
# to inherit from a built in class or object explicitly
def
test_instances_inherit_behavior_from_parent_class
(
self
):
chico
=
self
.
Chihuahua
(
"Chico"
)
self
.
assertEqual
(
__
,
chico
.
name
)
def
test_subclasses_add_new_behavior
(
self
):
chico
=
self
.
Chihuahua
(
"Chico"
)
self
.
assertEqual
(
__
,
chico
.
wag
())
fido
=
self
.
Dog
(
"Fido"
)
with
self
.
assertRaises
(
___
):
fido
.
wag
()
def
test_subclasses_can_modify_existing_behavior
(
self
):
chico
=
self
.
Chihuahua
(
"Chico"
)
self
.
assertEqual
(
__
,
chico
.
bark
())
fido
=
self
.
Dog
(
"Fido"
)
self
.
assertEqual
(
__
,
fido
.
bark
())
# ------------------------------------------------------------------
class
BullDog
(
Dog
):
def
bark
(
self
):
return
super
().
bark
()
+
", GRR"
# Note, super() is much simpler to use in Python 3!
def
test_subclasses_can_invoke_parent_behavior_via_super
(
self
):
ralph
=
self
.
BullDog
(
"Ralph"
)
self
.
assertEqual
(
__
,
ralph
.
bark
())
# ------------------------------------------------------------------
class
GreatDane
(
Dog
):
def
growl
(
self
):
return
super
().
bark
()
+
", GROWL"
def
test_super_works_across_methods
(
self
):
george
=
self
.
GreatDane
(
"George"
)
self
.
assertEqual
(
__
,
george
.
growl
())
# ---------------------------------------------------------
class
Pug
(
Dog
):
def
__init__
(
self
,
name
):
pass
class
Greyhound
(
Dog
):
def
__init__
(
self
,
name
):
super
().
__init__
(
name
)
def
test_base_init_does_not_get_called_automatically
(
self
):
snoopy
=
self
.
Pug
(
"Snoopy"
)
with
self
.
assertRaises
(
___
):
name
=
snoopy
.
name
def
test_base_init_has_to_be_called_explicitly
(
self
):
boxer
=
self
.
Greyhound
(
"Boxer"
)
self
.
assertEqual
(
__
,
boxer
.
name
)
Back
|
FazBrowse Home
|
New Git URL