FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python_koans/python2/koans/about_monkey_patching.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
/
python2
/
koans
/
about_monkey_patching.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (35 loc) · 1.35 KB
Breadcrumbs
python_koans
/
python2
/
koans
/
about_monkey_patching.py
Copy path
File metadata and controls
50 lines (35 loc) · 1.35 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Related to AboutOpenClasses in the Ruby Koans
#
from
runner
.
koan
import
*
class
AboutMonkeyPatching
(
Koan
):
class
Dog
(
object
):
def
bark
(
self
):
return
"WOOF"
def
test_as_defined_dogs_do_bark
(
self
):
fido
=
self
.
Dog
()
self
.
assertEqual
(
__
,
fido
.
bark
())
# ------------------------------------------------------------------
# Add a new method to an existing class.
def
test_after_patching_dogs_can_both_wag_and_bark
(
self
):
def
wag
(
self
):
return
"HAPPY"
self
.
Dog
.
wag
=
wag
fido
=
self
.
Dog
()
self
.
assertEqual
(
__
,
fido
.
wag
())
self
.
assertEqual
(
__
,
fido
.
bark
())
# ------------------------------------------------------------------
def
test_most_built_in_classes_cannot_be_monkey_patched
(
self
):
try
:
int
.
is_even
=
lambda
self
: (
self
%
2
)
==
0
except
StandardError
as
ex
:
self
.
assertMatch
(
__
,
ex
[
0
])
# ------------------------------------------------------------------
class
MyInt
(
int
):
pass
def
test_subclasses_of_built_in_classes_can_be_be_monkey_patched
(
self
):
self
.
MyInt
.
is_even
=
lambda
self
: (
self
%
2
)
==
0
self
.
assertEqual
(
____
,
self
.
MyInt
(
1
).
is_even
())
self
.
assertEqual
(
____
,
self
.
MyInt
(
2
).
is_even
())
Back
|
FazBrowse Home
|
New Git URL