关于《扩展阅读:Python Mixin 编程机制(转)》第三个插件方式的例子
class PlugIn(object):def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__ = f
def plugout(self, owner):
for f in self._exported_methods:
del owner.__dict__
class AFeature(PlugIn):
def __init__(self):
super(AFeature, self).__init__()
self._exported_methods.append(self.get_a_value)
def get_a_value(self):
print('a feature.')
class BFeature(PlugIn):
def __init__(self):
super(BFeature, self).__init__()
self._exported_methods.append(self.get_b_value)
def get_b_value(self):
print('b feature.')
class Combine:pass
c = Combine()
AFeature().plugin(c)
BFeature().plugin(c)
c.get_a_value()
c.get_b_value()
没有理解具体怎么运作,求大佬尽量详细解析。。。 c = Combine()
AFeature().plugin(c)
class AFeature(PlugIn):
def __init__(self):
super(AFeature, self).__init__()
class PlugIn(object):
def __init__(self):
self._exported_methods = []
self._exported_methods.append(self.get_a_value)
def plugin(self, owner): #这里的owner,就是Combine的实例化对象
for f in self._exported_methods:
owner.__dict__ = f Stubborn 发表于 2020-6-5 12:35
大致能理解,感谢
页:
[1]