简体   繁体   English

Python中类foo和类foo(对象)之间的区别

[英]Difference between class foo and class foo(object) in Python

I know class foo(object) is an old school way of defining a class. 我知道class foo(object)是一种定义类的旧式学习方式。 But I would like to understand in more detail the difference between these two. 但我想更详细地了解这两者之间的区别。

Prior to python 2.2 there were essentially two different types of class: Those defined by C extensions and C coded builtins (types) and those defined by python class statements (classes). 在python 2.2之前,基本上有两种不同类型的类:由C扩展和C编码内置(类型)定义的类和由python类语句(类)定义的类。 This led to problems when you wanted to mix python-types and builtin types. 当你想混合python-types和内置类型时,这会导致问题。 The most common reason for this is subclassing. 最常见的原因是子类化。 If you wanted to subclass the list type in python code, you were out of luck, and so various workarounds were used instead, such as subclassing the pure python implementation of lists (in the UserList module) instead. 如果你想在python代码中继承列表类型,那你就运气不好,因此使用了各种变通方法,比如子类化列表的纯python实现(在UserList模块中)。

This was a fairly ugly, so in 2.2 there was a move to unify python and builtin types, including the ability to inherit from them. 这是一个相当难看的,所以在2.2中有一个统一python和内置类型的举动 ,包括从它们继承的能力。 The result is "new style classes". 结果是“新风格”。 These do have some incompatible differences to old-style classes however, so for backward compatability the bare class syntax creates an old-style class, while the new behaviour is obtained by inheriting from object. 这些与旧式类有一些不兼容的差异,因此对于向后兼容性,裸类语法创建旧式类,而新行为是通过从对象继承获得的。 The most visible behaviour differences are: 最明显的行为差异是:

  • The method resolution order (MRO). 方法解析顺序(MRO)。 There is a difference in behaviour in diamond-shaped inheritance hierarchies (where A inherits from both B and C, which both inherit from a common base class D. Previously, methods were looked up left-to right, depth first (ie ABDCD) However if C overloads a member of D, it won't be used by A (as it finds D's implementation first) This is bad for various styles of programming (eg. using mixin classes). New style classes will treat this situation as ABCD, (look at the __mro__ attribute of a class to see the order it will search) 菱形继承层次结构中的行为存在差异(其中A继承自B和C,它们都从公共基类D继承。以前,方法从左到右查找,深度优先(即ABDCD)但是如果C重载了D的一个成员,它将不会被A使用(因为它首先发现D的实现)这对于各种编程风格(例如使用mixin类)是不好的。新样式类将这种情况视为ABCD, (查看类的__mro__属性以查看它将搜索的顺序)

  • The __new__ constructor is added, which allows the class to act as a factory method, rather than return a new instance of the class. 添加了__new__构造函数,它允许类充当工厂方法,而不是返回类的新实例。 Useful for returning particular subclasses, or reusing immutable objects rather than creating new ones without having to change the creation interface. 用于返回特定子类,或重用不可变对象而不是创建新对象而无需更改创建接口。

  • Descriptors . 描述符 These are the feature behind such things as properties, classmethods, staticmethods etc. Essentially, they provide a way to control what happens when you access or set a particular attribute on a (new style) class. 这些是属性,类方法,静态方法等背后的特征。本质上,它们提供了一种方法来控制在(新样式)类上访问或设置特定属性时发生的事情。

class foo(object): is the 'new' way of declaring classes. class foo(object):是声明类的'新'方式。

This change was made in python 2.2, see this PEP for an explanation of the differences. 这个更改是在python 2.2中进行的,请参阅此PEP以了解差异的解释

Subclassing object yields a new-style class. 子类化object产生一个新式的类。 Two well known advantages of new-style classes are: 新式课程的两个众所周知的优点是:

  • Metaclasses (like class factories, but works transparently) 元类(类工厂,但透明地工作)
  • Properties (getters & setters...) 属性(getter&setters ...)

引用这个类Foo(object)中的对象是为了使你的python 3代码与python 2和3兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM