阿☆輝 发表于 2020-5-26 20:33:44

经过多次试验,Python3中__bases__属性实现给类A添加类B特性的条件是:
A、B分别是一个继承类,且继承于不同的基类

【实例代码如下:】
class X:
        pass

class A(X):
        def get_a(self):
                print('a')


class Y:
        pass

class B(Y):
        def get_b(self):
                print('b')

A.__bases__
B.__bases__
A.__bases__ += (B,)
a = A()
a.get_b()

zaihebian 发表于 2020-6-3 09:53:42

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()



问:super(AFeature, self)和super(BFeature, self)这两个参数为什么要放,我改成super().__init__() 运行结果是一样的,请问这里有什么区别呢??????

嚣张的稀粥 发表于 2020-6-3 15:04:06

{:10_269:}还需努力,噶油

路过打酱油 发表于 2020-6-17 09:50:22

最后这个看了半天居然没有答案,个人理解是c实例化什么都没有,但是通过插入方法把其他类的方法插入到了C的dict从而扩展了c,这样c就拥有了get_a_value和b_get_b_value方法

jy01388519 发表于 2020-6-21 16:56:18

C.__bases__ +=(A,B,)报错啊,3.8.3

嘉岳呀 发表于 2020-7-17 12:24:46

小甲鱼是大帅锅!!!

大周家的MSH 发表于 2020-7-19 21:13:04

请兄弟们帮助我理解下第三段代码吧,BALLBALL!

Chreval 发表于 2020-8-12 05:22:12

学习!感谢!

FindingB 发表于 2020-8-12 18:19:19

不懂(=_=)

y116114 发表于 2020-9-20 17:08:50

好蒙。。。
{:10_266:}

linzch 发表于 2020-9-26 19:41:33

为什么在Python中用bases会报错

风风魔王 发表于 2020-10-6 15:25:33

cashchen17909 发表于 2017-7-27 13:53
f.__name__是啥意思呀。。哪位帮忙解释下。。

同问,这里看不懂,__name__一般不都是__main__么

风风魔王 发表于 2020-10-6 15:38:02

darksolitary 发表于 2019-2-18 12:12
# __bases__方法是查看某个类继承的所有父类
在python3.0+上(我也是猜测:因为我用的是小甲鱼推荐的3.0+ ...

请问f.__name__是什么呀?

风风魔王 发表于 2020-10-6 15:53:09

zaihebian 发表于 2020-6-3 09:53
class PlugIn(object):
    def __init__(self):
      self._exported_methods = []


同问,请问您知道了吗?

认真学好py 发表于 2020-10-12 19:54:21

哪位同学的vip号能借我一下 我真的想学习 好人一生平安 ,没有恶意

杏仁好伐 发表于 2020-10-27 14:43:47

python3不行
>>> A.__bases__ += (B,)
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
    A.__bases__ += (B,)
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, B
>>> class C:
        pass

>>> C.__bases__ += (A, B, )
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
    C.__bases__ += (A, B, )
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, A, B
>>> B.__bases__ += (A, )
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
    B.__bases__ += (A, )
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, A

CorrineLL 发表于 2020-11-4 22:44:04

不懂

大宝陈 发表于 2020-11-7 19:38:43

小甲鱼老师,为啥示范代码是Python2.X的语法?

tang.nb 发表于 2020-11-24 19:28:03

a feature.
b feature.

tang.nb 发表于 2020-11-24 19:28:39

tang.nb 发表于 2020-11-24 19:28
a feature.
b feature.

结果是这个 ---第三个的
页: 1 2 3 4 5 [6] 7
查看完整版本: Python Mixin 编程机制(转)