鱼C论坛

 找回密码
 立即注册
查看: 2946|回复: 10

python __bases__ 咋用?

[复制链接]
发表于 2014-9-17 22:26:28 | 显示全部楼层 |阅读模式
30鱼币
本帖最后由 欣欣celin 于 2014-10-22 15:31 编辑

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

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


class C:
    pass

C.__bases__ +=(A,B, )

以上运行时报错:Cannot create a consistent method resolution
order (MRO) for bases object, A, B

咋整吖,求大神支招

最佳答案

查看完整内容

看你发了那么多贴 你才是大神把。我也是懵懵懂懂 刚玩不久 >>> class A: a = 1 >>> class B: b = 1 >>> class C(A,B): pass >>> C.__base__ >>> C.__bases__ (, ) >>> C.a 1 >>> C.b 1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-17 22:26:29 | 显示全部楼层
看你发了那么多贴 你才是大神把。我也是懵懵懂懂 刚玩不久
>>> class A:
        a = 1

       
>>> class B:
        b = 1

       
>>> class C(A,B):
        pass

>>> C.__base__
<class '__main__.A'>
>>> C.__bases__
(<class '__main__.A'>, <class '__main__.B'>)
>>> C.a
1
>>> C.b
1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-18 10:48:03 | 显示全部楼层
http://www.douban.com/note/335616605/
百度了下,是这个,希望对你有用,反正我貌似明白了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-9-18 21:55:09 | 显示全部楼层
swl19880916 发表于 2014-9-18 10:48
http://www.douban.com/note/335616605/
百度了下,是这个,希望对你有用,反正我貌似明白了。

太菜,看不懂
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-22 15:30:03 | 显示全部楼层
蒸发,回来....还是没解,更菜了,咋办????
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-24 22:42:46 | 显示全部楼层
我是过来学习的, 受教了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-24 23:19:30 | 显示全部楼层
。。。你的class C并没有继承A和B啊。如果你class C(A,B):pass 然后再打印出C.__bases__的话,你大概就知道了,是指它的父类。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-10-28 15:39:26 | 显示全部楼层
吴聊 发表于 2014-10-24 23:19
。。。你的class C并没有继承A和B啊。如果你class C(A,B):pass 然后再打印出C.__bases__的话,你大概就知道 ...

.......

class C(A, B):
    pass

C.__bases__      #改成这样,运行如下


>>> ============= RESTART ===============
>>>
>>> f=C
>>> f
<class '__main__.C'>
>>> f.__main__
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    f.__main__
AttributeError: type object 'C' has no attribute '__main__'
>>> f.bases
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    f.bases
AttributeError: type object 'C' has no attribute 'bases'


那么,我改怎么改才能实现‘多继承’呢?
有劳大神指点指点,万分感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-11-5 15:01:27 | 显示全部楼层
吴聊 发表于 2014-9-17 22:26
看你发了那么多贴 你才是大神把。我也是懵懵懂懂 刚玩不久
>>> class A:
        a = 1

大神吖,看AI家欠缺慧根,学到这里就被卡死了

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-24 11:19:14 | 显示全部楼层
C.__bases__(A,B,)这种多继承的实现方法,在Python 3之后就摒弃了。这是我的个人理解。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-25 09:09:37 | 显示全部楼层
太菜   看不懂哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-16 03:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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