原来如此。class PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
i = 0
for f in self._exported_methods:
owner.__dict__[f.__name__] = f
i += 1
print(f,i)
def plugout(self, owner):
for f in self._exported_methods:
del owner.__dict__[f.__name__]
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()
结果:
>>>
RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python35-32/7.py
<bound method AFeature.get_a_value of <__main__.AFeature object at 0x0219A070>> 1
<bound method BFeature.get_b_value of <__main__.BFeature object at 0x01B976B0>> 1
a feature.
b feature.
>>> |