简体   繁体   English

在Python中,“class name(object):”和“class name():”之间有什么区别?

[英]In Python, what is the difference between “class name(object):” and “class name():”

What is the difference between the two classes below? 下面两个类有什么区别? Do you have some related information about this case? 你有关于这个案子的一些相关信息吗? Thank you very much. 非常感谢你。

class test(object):
  def __init__(self, name):
     print name

class test():
  def __init__(self, name):
     print name

In python 2.x, the class that inherits from object will be a new-style class, while the other won't, while in python 3.x there'll be both new-style. 在python 2.x中,从object继承的类将是一个新式的类,而另一个不会,而在python 3.x中,将同时存在新的类。

However, the differences between new and old are rather advanced, (for example, attribute search order) so a beginner shouldn't be too concerned about the incompatibilities. 但是,新旧之间的差异相当高级(例如,属性搜索顺序),因此初学者不应过于关注不兼容性。

See this answer for more information if you're interested, but it's rather a thing for library developers etc. 如果您感兴趣,请参阅答案以获取更多信息,但这对于图书馆开发人员来说更是如此。

Mhmm ... this wiki-page explains the differences very illustratively: http://wiki.python.org/moin/NewClassVsClassicClass 嗯...这个维基页面非常说明性地解释了这些差异: http ://wiki.python.org/moin/NewClassVsClassicClass

And I saw some answeres with the information, that Old-(Classic)-Style and New-Style classes are the same in py3 -> that's not correct: 我看到一些回复信息,老 - (经典) - 风格和新风格的类在py3中是相同的 - >这是不正确的:

在此输入图像描述 Old-style classes are removed in Python 3, leaving only the semantics of new-style classes 在Python 3 中删除了旧式类,只留下了新式类的语义

Besides this, the New-Style classes are quite available since Python 2.2. 除此之外,自从Python 2.2以来, New-Style类已经非常可用了。 Up to 2.1 we have had to use the Classic style -> see here 高达2.1我们不得不使用经典风格- >见这里

Short summary about the differences/infos could be: 关于差异/信息的简短摘要可能是:

  • New-Style classes are available since Python 2.2 从Python 2.2开始,新的类可用
  • New-Style classes can use descriptors - Old Style classes cannot 新样式类可以使用描述符 - 旧样式类不能
  • New Style classes can subclass most built-in types - Old Style classes cannot 新样式类可以子类化大多数内置类型 - 旧样式类不能
  • New Style classes supports a new meta-model (which affects eg the behaviour of the type() built-in massively) 新的Style类支持一个新的元模型(它会影响大量内置的type()的行为)
  • Old-Style classes will find an attribute on an instance before it looks in the hierarchy - New-Style classes will let the class definition win if it is a writeable descriptor 旧式类在查找层次结构之前会在实例上找到一个属性 - 如果类型是可写描述符,则New-Style类将让类定义获胜
  • Old-Style classes has been removed in Python 3 Python 3中删除了旧式类

But in most way the introduction of the New-style classes has been affected within the comming up of python's Descriptors --> read more here . 但是在大多数情况下,新风格类的引入已经在python的描述符的组合中受到影响- >在这里阅读更多内容

在这种情况下,没有,因为第一个显式地从对象继承为基类,而第二个隐式地从对象继承。

暂无
暂无

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

相关问题 Python字典类中self[name]和self.__dict__[name]有什么区别? - What is difference between self[name] and self.__dict__[name] in Python dictionary class? python class 创建的语法差异 - class name_class: class name_class(): 和 class name_class(Object): - difference in syntax of python class creation - class name_class: class name_class(): and class name_class(Object): 在Python中使用与脚本名称相同的类名称会有什么区别? - What difference will it make to use same class name as script name in Python? 之间的差异 <class name> 。 <var name>和自我。</var> <var name><var name>在python类中</var></var> - difference between <class name>. <var name> and self.<var name> in a python class Python中的“ A类:”和“ A(object):类”有什么区别? - What's the difference between 'class A:' and 'class A(object):' in Python? python 中的 class 和 object class 类型有什么区别 - what is the difference between type class and object class in python Python中类名后面括号之间的区域叫什么名字? - What is the name of the region between parentheses after the class name in Python? super()和Parent类名之间有什么区别? - What's the difference between super() and Parent class name? 这两种情况之间有什么区别:传递类名与对象 - Is there any difference between these two cases: passing class name vs object Python:将 lambda 函数和类名作为参数传递之间的区别 - Python: Difference between passing lambda function and class name as arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM