class ClassName与class ClassName(object) [英] class ClassName versus class ClassName(object)

查看:207
本文介绍了class ClassName与class ClassName(object)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两者之间的区别:

class ClassName(object):
    pass

class ClassName:
    pass



当我调用这些类的模块的帮助功能时, ____ builtin ____。object 用于第一种情况下的帮助的CLASS标题。对于第二种情况,它只显示类名。

When I call the help function of the module of those class you can read ____builtin____.object for the first case just under the CLASS title of help. For the second case it just shows the class name. Is there any functional difference between those classes and/or possible methods thereof?

(我知道 class Classname(ParentClassName)有一个功能用途)

(I know that class Classname(ParentClassName) has a functional use)

推荐答案

当你从object继承时,你的类是一个新风格在Python 2.2中实现(2001年左右) - 从对象案例的非继承创建一个旧样式类,实际上只维持向后兼容性。

When you inherit from "object" you class is a "new style" class - that was implemented back in Python 2.2 (around 2001) - The non inheriting from "object" case creates an "old style" class, that was actually maintained only for backwards compatibility.

新风格类的巨大好处是跨Python的类型的统一 - 在此之前,不能正确子类化内置类型,如int,list,dict。还指定了描述符协议,其描述用于在对象中检索和设置属性的协议,给予语言大量的灵活性。 (当一个类在类中使用Python属性时,它是更可见的)。

The great benefit of "new style" classes is the unification of types across Python - prior to that, one could not subclass built-in types such as int, list, dict, properly. There was also specified a "descriptor protocol" which describes a protocol for retrieving and setting attributes in an object, giving the language a lot of flexibility. (It is more visible when one does use a Python "property" in a class).

有什么区别实际上不是从对象继承,因为Python中的类也是对象,这改变了类的类本身(类的类被称为元类)。因此,如果您将元类设置为type,您不需要继承对象就可以拥有一个新的样式类。

What does make the difference is not actually "inheriting from object", but, since classes in Python are also objects, that does change the class'class itself (a class'class is known as its "metaclass"). Thus if you set the metaclass to be "type", you don't need to inherit from object to have a new style class.

这是 ,在Python 2.x中,所有的类都是新样式 - 使用旧样式类可能适用于一些简单的情况,但它们可以生成很多细微,难以找到的错误,当您尝试使用属性,pickle,描述符和其他高级功能。首先,当您尝试检查对象的类型时,即使它们来自不同的用户定义类,对于旧样式类的所有对象也是一样的(类型实例)。

It is strongly recommended that in Python 2.x, all your classes are new style - using old style classes may work for some single straightforward cases, but they can generate a lot of subtle, difficult to find, errors, when you try to use properties, pickle, descriptors, and other advanced features. Above all, when you try to check the "type" of an object, it will be the same (type "instance") for all objects from old style classes, even if they are from different user defined classes.

在Python版本3.x中,所有类都是新样式 - 无需设置元类。

In Python versions 3.x all classes are new style - no need to set the metaclass.

Python的文档datamodel详细定义了
类typs的行为的法律书(足以允许重新实现该语言):

Python's documentation "datamodel" is the "book of law" where the behavior of both class typs is defined in detail (enough to allow one to reimplement the language):

http://docs.python.org/reference/datamodel.html

这篇来自Guido的博客介绍了一个更轻的语言中的新风格类的动机:

This blog post from Guido talks about the motivations behind new style classes in a lighter language:

http://python-history.blogspot.com.br/2010/06/new-style- categories.html

http://docs.python.org/release/2.5.2/ref/node33.html

这篇关于class ClassName与class ClassName(object)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆