testpachong
V2EX  ›  问与答

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

  •  
  •   testpachong · Aug 1, 2016 · 2562 views
    This topic created in 3573 days ago, the information mentioned may be changed or developed.
    4 replies    2016-08-01 18:23:00 +08:00
    zhanglintc
        1
    zhanglintc  
       Aug 1, 2016
    这段代码不是会报错吗?
    ipconfiger
        2
    ipconfiger  
       Aug 1, 2016   ❤️ 1
    2.7 下

    3.5.1
    kunimi
        3
    kunimi  
       Aug 1, 2016
    unbound method 概念在 python 3 中被移除了,在 python 3 中,使用 def 定义的方法其实就是一个普通的 function 。所以你这段代码在 python 3 中可以运行但是在 2 中会报错。
    hahastudio
        4
    hahastudio  
       Aug 1, 2016
    你这个根本没有继承啊
    你遇到的应该是 Bound method, Unbound method, Class method, Static method 之间的关系。
    你想调用的是一个 Unbound method , self 里给的是另一个 class 的 instance ,这样有些危险。
    继承的声明是这样的
    class DerivedClassName(Base1, Base2, Base3):
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3228 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 13:27 · PVG 21:27 · LAX 06:27 · JFK 09:27
    ♥ Do have faith in what you're doing.