testpachong
V2EX  ›  问与答

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

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

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