Python中的旧样式类和新样式类有什么区别?什么时候我应该用一种或另一种?


当前回答

Declaration-wise:

New-style类继承自object或另一个New-style类。

class NewStyleClass(object):
    pass

class AnotherNewStyleClass(NewStyleClass):
    pass

老式的类没有。

class OldStyleClass():
    pass

注意事项:

Python 3不支持旧样式的类,所以上面提到的任何一种形式都会产生新样式的类。

其他回答

从新型和经典类:

Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.__class__ designates the class of x, but type(x) is always <type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance. New-style classes were introduced in Python 2.2 to unify the concepts of class and type. A new-style class is simply a user-defined type, no more, no less. If x is an instance of a new-style class, then type(x) is typically the same as x.__class__ (although this is not guaranteed – a new-style class instance is permitted to override the value returned for x.__class__). The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model. It also has a number of immediate benefits, like the ability to subclass most built-in types, or the introduction of "descriptors", which enable computed properties. For compatibility reasons, classes are still old-style by default. New-style classes are created by specifying another new-style class (i.e. a type) as a parent class, or the "top-level type" object if no other parent is needed. The behaviour of new-style classes differs from that of old-style classes in a number of important details in addition to what type returns. Some of these changes are fundamental to the new object model, like the way special methods are invoked. Others are "fixes" that could not be implemented before for compatibility concerns, like the method resolution order in case of multiple inheritance. Python 3 only has new-style classes. No matter if you subclass from object or not, classes are new-style in Python 3.

新的样式类可以使用super(Foo, self),其中Foo是类,self是实例。

超级(类型、对象或者类型) 返回一个代理对象,该对象将方法调用委托给类型的父类或兄弟类。这对于访问在类中被重写的继承方法非常有用。除了类型本身被跳过之外,搜索顺序与getattr()所使用的相同。

在Python 3中。X你可以简单地在类中使用super()而不带任何参数。

Guido写了关于新型类的内幕,这是一篇关于Python中的新型和老式类的很棒的文章。

Python 3只有new-style类。即使你写了一个“老式类”,它也是隐式地从object派生的。

新型类具有一些老式类所缺乏的高级特性,如super,新的C3 mro,一些神奇的方法等。

Declaration-wise:

New-style类继承自object或另一个New-style类。

class NewStyleClass(object):
    pass

class AnotherNewStyleClass(NewStyleClass):
    pass

老式的类没有。

class OldStyleClass():
    pass

注意事项:

Python 3不支持旧样式的类,所以上面提到的任何一种形式都会产生新样式的类。

New-style类继承自object,并且在Python 2.2以后必须这样编写(即类Classname(object):而不是类Classname:)。核心变化是统一类型和类,这样做的好处是允许从内置类型继承。

更多细节请阅读描述介绍。