V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
testpachong
V2EX  ›  问与答

python 继承不需要填写父类就可以直接使用吗?

  •  
  •   testpachong · 2016-08-01 15:13:09 +08:00 · 1867 次点击
    这是一个创建于 2838 天前的主题,其中的信息可能已经有所发展或是发生改变。
    4 条回复    2016-08-01 18:23:00 +08:00
    zhanglintc
        1
    zhanglintc  
       2016-08-01 15:25:24 +08:00
    这段代码不是会报错吗?
    ipconfiger
        2
    ipconfiger  
       2016-08-01 15:33:59 +08:00   ❤️ 1
    2.7 下

    3.5.1
    kunimi
        3
    kunimi  
       2016-08-01 16:02:13 +08:00
    unbound method 概念在 python 3 中被移除了,在 python 3 中,使用 def 定义的方法其实就是一个普通的 function 。所以你这段代码在 python 3 中可以运行但是在 2 中会报错。
    hahastudio
        4
    hahastudio  
       2016-08-01 18:23:00 +08:00
    你这个根本没有继承啊
    你遇到的应该是 Bound method, Unbound method, Class method, Static method 之间的关系。
    你想调用的是一个 Unbound method , self 里给的是另一个 class 的 instance ,这样有些危险。
    继承的声明是这样的
    class DerivedClassName(Base1, Base2, Base3):
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3901 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:17 · PVG 12:17 · LAX 21:17 · JFK 00:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.