rtl1998
发表于 2018-8-27 10:21:17
先记下
张佳明0502
发表于 2018-10-31 19:43:49
__bases__的两种方式运行出错,插件看不懂{:10_266:}
arronking
发表于 2018-11-1 21:23:57
18853068 发表于 2015-8-20 16:06
Python 2下运行通过。。Python3下不行
额,sad,还浪费了我好一会儿
云中锦鹤
发表于 2018-11-4 10:33:26
加油,不放弃
薄荷的琦姑娘
发表于 2018-11-14 11:46:18
第三个看不懂,难过。
Lidapao
发表于 2018-11-15 22:24:21
本帖最后由 Lidapao 于 2018-11-15 22:33 编辑
第二个方法__bases__ 在python3中好像不能使用。
解释一下插件方式:
因为AFeature 和BFeature都是继承自PlugIn的,所以他们中也有plugin 和 plugout方法(接下来就以AFeature类进行解释,BFeature和AFeature一样)
AFeature().plugin(c) 实则是运行了以下代码
for f in AFeature._exported_methods: #AFeature中存放get_a_value的列表
c.__dict__[ get_a_value] = f # 字典中给一个不存在的keys赋值会创建新的keys和对应value之前字典中讲过
给c.__dict__中添加get_a_value方法
因为在AFeature.__init__中初始化了PlugIn的__init__所以他们中也有_exported_methods列表,就是存放方法的列表
在这个列表中存放的是get_a_value这个方法。
所以c中就有了get_a_value方法
小谢520
发表于 2018-11-24 09:54:54
{:5_106:}
MrClown
发表于 2018-11-25 17:04:12
本帖最后由 MrClown 于 2018-11-25 17:05 编辑
唉
熠熠生辉
发表于 2019-1-14 11:06:16
最后一张的代码写的太好了,一目了然
Amor167
发表于 2019-1-14 22:05:59
先马后看
dereksy
发表于 2019-1-23 15:45:49
说明一下,最后一段代码:
1、是通过修改类对象Combine之引用c的字典键值,获得AFeature的方法get_a_value
2、这段代码的目的是:只想针对类对象的引用c进行扩展方法。而不要用多继承或__base__那样,直接把Afeature的方法属性直接继承给Combine类。
darksolitary
发表于 2019-2-18 11:58:13
非常感谢甲鱼大佬对于,Mix-in的拓展【即使,编程属于已过一段时间,但是内容也是很值得看得】
darksolitary
发表于 2019-2-18 12:06:44
本帖最后由 darksolitary 于 2019-2-18 20:24 编辑
# __bases__方法:在python3.+以上,可以是查看某个类继承的所有父类【但是修改的话不行】,
亲测(编译器:3.0+),附代码:
lass PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__ = f
class AFeature(PlugIn):
def __init__(self):
super().__init__()
self._exported_methods.append(self.get_a_value)
def get_a_value(self):
print ('a feature.')
class BFeature(PlugIn):
def __init__(self):
super().__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()
AFeature.__bases__ += (BFeature) #这行注释掉,就没错
print(AFeature.__bases__)
darksolitary
发表于 2019-2-18 12:12:49
# __bases__方法是查看某个类继承的所有父类
在python3.0+上(我也是猜测:因为我用的是小甲鱼推荐的3.0+版本),是可以实现上述功能的,但是却无法实现添加父类功能
附上代码:
class PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__ = f
class AFeature(PlugIn):
def __init__(self):
super().__init__()
self._exported_methods.append(self.get_a_value)
def get_a_value(self):
print ('a feature.')
class BFeature(PlugIn):
def __init__(self):
super().__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()
AFeature.__bases__ += (BFeature) #测试是否可添加新父类,报错
print(AFeature.__bases__) #测试是否可以查看所有父类,可以
darksolitary
发表于 2019-2-18 12:19:22
本帖最后由 darksolitary 于 2019-2-18 12:23 编辑
lass PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__ = f#将已存储完的函数名列表中的数据,通过循环添加到“owner”所拥有的__dict__(我理解是:用于存储自身方法的“字典类型:的属性),进而完成对象方法扩展
class AFeature(PlugIn):
def __init__(self):
super().__init__()#创建能存储自己所含的函数名列表
self._exported_methods.append(self.get_a_value) #存储
def get_a_value(self):
print ('a feature.')
class BFeature(PlugIn):
def __init__(self):
super().__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()
AFeature.__bases__ += (BFeature)
print(AFeature.__bases__)
darksolitary
发表于 2019-2-18 12:23:53
lass PlugIn(object):
def __init__(self):
self._exported_methods = []
def plugin(self, owner):
for f in self._exported_methods:
owner.__dict__ = f#将已存储完的函数名列表中的数据,通过循环添加到“owner”所拥有的__dict__(我理解是:用于存储自身方法的“字典类型:的属性),进而完成对象方法扩展
class AFeature(PlugIn):
def __init__(self):
super().__init__()#创建能存储自己所含的函数名列表
self._exported_methods.append(self.get_a_value) #存储
def get_a_value(self):
print ('a feature.')
class BFeature(PlugIn):
def __init__(self):
super().__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()
AFeature.__bases__ += (BFeature)
print(AFeature.__bases__)
fishzang
发表于 2019-3-20 21:29:50
几个意思啊?不明觉厉啊
fishzang
发表于 2019-3-20 22:02:22
本帖最后由 fishzang 于 2019-3-20 22:03 编辑
super(AFeature, self).__init__()
self 不是放在方法的第一个吗? 为什么前面加了一个 “AFeature”类名?
一知半解
发表于 2019-5-30 17:41:13
希望 小甲鱼 快点更新 有些代码对不上了 教程老了 不过 核心不变
中科院王俊凯
发表于 2019-6-7 17:25:34
目前阻力比较大,应该多学几遍