【问题标题】:What's the difference between 'class A:' and 'class A(object):' in Python?Python中的'class A:'和'class A(object):'有什么区别?
【发布时间】:2010-12-27 00:08:46
【问题描述】:

有什么区别

class A:
    pass

class B(object):
    pass

?出于某种原因,在方法中我不能使用super(A, self),但super(B, self) 效果很好。我猜 Py3k 中没有这种特殊性 :)

【问题讨论】:

    标签: python class inheritance python-3.x


    【解决方案1】:

    第一个是旧样式类。二是新式班。请参阅http://docs.python.org/tutorial/classes.html#multiple-inheritance 了解有关差异的详细讨论。 super() 仅适用于新式类。 http://docs.python.org/library/functions.html#super

    【讨论】:

    • 嗯,这就是为什么人们建议我先学习 Python2.. :))) 我会尽快接受这个
    【解决方案2】:

    在 2.x 中,后者创建一个new-style class。在 3.x 中,两者具有相同的效果,因为旧式类已被删除。

    【讨论】:

      【解决方案3】:

      B类是一个新样式类http://www.python.org/doc/newstyle/

      【讨论】:

        【解决方案4】:

        如前所述,第二种情况创建了一个新样式的类,而第一种情况创建了一个旧样式的类(已弃用!)。

        为消除旧样式的限制而创建的新样式类:旧样式类不能从内置类型继承。 使用新式类,您可以从内置类型继承;事实上,所有内置类型都派生自“对象”:

        >>> list.__mro__
        (<type 'list'>, <type 'object'>)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多