[英]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: 关于差异/信息的简短摘要可能是:
type()
built-in massively) 新的Style类支持一个新的元模型(它会影响大量内置的type()
的行为) 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.