鱼C论坛

 找回密码
 立即注册
查看: 778|回复: 2

[已解决]关于《扩展阅读:Python Mixin 编程机制(转)》第三个插件方式的例子

[复制链接]
发表于 2020-6-5 10:58:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. class PlugIn(object):
  2.     def __init__(self):
  3.         self._exported_methods = []
  4.         
  5.     def plugin(self, owner):
  6.         for f in self._exported_methods:
  7.             owner.__dict__[f.__name__] = f

  8.     def plugout(self, owner):
  9.         for f in self._exported_methods:
  10.             del owner.__dict__[f.__name__]

  11. class AFeature(PlugIn):
  12.     def __init__(self):
  13.         super(AFeature, self).__init__()
  14.         self._exported_methods.append(self.get_a_value)

  15.     def get_a_value(self):
  16.         print('a feature.')

  17. class BFeature(PlugIn):
  18.     def __init__(self):
  19.         super(BFeature, self).__init__()
  20.         self._exported_methods.append(self.get_b_value)

  21.     def get_b_value(self):
  22.         print('b feature.')
  23.         

  24. class Combine:pass

  25. c = Combine()
  26. AFeature().plugin(c)
  27. BFeature().plugin(c)

  28. c.get_a_value()
  29. c.get_b_value()
复制代码


没有理解具体怎么运作,求大佬尽量详细解析。。。
最佳答案
2020-6-5 12:35:12
  1. c = Combine()
  2. AFeature().plugin(c)

  3. class AFeature(PlugIn):
  4.     def __init__(self):
  5.         super(AFeature, self).__init__()

  6. class PlugIn(object):
  7.     def __init__(self):
  8.         self._exported_methods = []

  9. self._exported_methods.append(self.get_a_value)

  10.     def plugin(self, owner): #  这里的owner,就是Combine的实例化对象
  11.         for f in self._exported_methods:
  12.             owner.__dict__[f.__name__] = f
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-5 12:35:12 | 显示全部楼层    本楼为最佳答案   
  1. c = Combine()
  2. AFeature().plugin(c)

  3. class AFeature(PlugIn):
  4.     def __init__(self):
  5.         super(AFeature, self).__init__()

  6. class PlugIn(object):
  7.     def __init__(self):
  8.         self._exported_methods = []

  9. self._exported_methods.append(self.get_a_value)

  10.     def plugin(self, owner): #  这里的owner,就是Combine的实例化对象
  11.         for f in self._exported_methods:
  12.             owner.__dict__[f.__name__] = f
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-5 14:39:21 | 显示全部楼层

大致能理解,感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 10:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表