轻松掌握python设计模式之访问者模式
本文实例为大家分享了python访问者模式代码,供大家参考,具体内容如下
"""访问者模式""" 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 """python支持多重继承,在解析父类的__init__时,定义解析顺序的是子类的__mro__属性,内容为一个存储要解析类顺序的元组。""" """观察到,super的执行路径和类的__mro__列举的类顺序吻合;而__mro__的顺序可以看作是深搜的结果""" for cls in node.__class__.__mro__: """方法名""" meth_name = 'visit_' + cls.__name__ """getattr()函数是Python自省的核心函数,具体使用大体如下: 获取对象引用getattr,Getattr用于返回一个对象属性,或者方法 如果Visitor对象中有属性meth_name则获得方法返回的值,否则赋值None """ 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('通常访问: ' + node.__class__.__name__) def visit_B(self, node, *args, **kwargs): print('访问_B ' + node.__class__.__name__) a = A() b = B() c = C() visitor = Visitor() visitor.visit(a) visitor.visit(b) visitor.visit(c)
运行结果如图:
相关推荐
TingBen 2020-09-16
Kakoola 2020-01-12
Clevebaby 2012-04-23
New丶Elements 2019-06-27
AHRL 2011-02-17
守望丶 2011-01-19
Backwardelements 2010-12-18
guojing 2010-01-27
lujingjing 2016-05-03
fancyler 2018-02-05
thousfeet 2015-07-23
githubphpnobug 2019-04-17
PHP100 2019-03-28
vczh的日常 2018-04-21
前端外刊评论 2018-03-05
MATLAB 2017-11-28