FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/visitor.py at master · pythug/python-patterns · GitHub
pythug
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
visitor.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (35 loc) · 938 Bytes
Breadcrumbs
python-patterns
/
visitor.py
Copy path
File metadata and controls
52 lines (35 loc) · 938 Bytes
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
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html"""
class
Node
(
object
):
pass
class
A
(
Node
):
pass
class
B
(
Node
):
pass
class
C
(
A
,
B
):
pass
class
Visitor
(
object
):
def
visit
(
self
,
node
,
*
args
,
**
kwargs
):
meth
=
None
for
cls
in
node
.
__class__
.
__mro__
:
meth_name
=
'visit_'
+
cls
.
__name__
meth
=
getattr
(
self
,
meth_name
,
None
)
if
meth
:
break
if
not
meth
:
meth
=
self
.
generic_visit
return
meth
(
node
,
*
args
,
**
kwargs
)
def
generic_visit
(
self
,
node
,
*
args
,
**
kwargs
):
print
(
'generic_visit '
+
node
.
__class__
.
__name__
)
def
visit_B
(
self
,
node
,
*
args
,
**
kwargs
):
print
(
'visit_B '
+
node
.
__class__
.
__name__
)
a
=
A
()
b
=
B
()
c
=
C
()
visitor
=
Visitor
()
visitor
.
visit
(
a
)
visitor
.
visit
(
b
)
visitor
.
visit
(
c
)
### OUTPUT ###
# generic_visit A
# visit_B B
# visit_B C
Back
|
FazBrowse Home
|
New Git URL