Python类继承对象 [英] Python class inherits object
问题描述
是否有任何理由让类声明继承自 object
?
Is there any reason for a class declaration to inherit from object
?
我刚刚找到一些代码这是我找不到一个很好的理由为什么。
I just found some code that does this and I can't find a good reason why.
class MyClass(object):
# class code follows...
推荐答案
风格对象。这是在python2.2中引入的一个特性。
Yes, this is a 'new style' object. It was a feature introduced in python2.2.
新风格对象具有与经典对象不同的对象模型,有些东西不能与旧风格对象正常工作,例如,super(),@property和描述符。请参阅这篇文章,了解新风格类的详细说明:
New style objects have a different object model to classic objects, and some things won't work properly with old style objects, for instance, super(), @property and descriptors. See this article for a good description of what a new style class is:
http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html
SO链接,了解区别说明: http:// stackoverflow .com / questions / 54867 / old-style-and-new-style-classes-in-python
SO link for a description of the differences: http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python
这篇关于Python类继承对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!