Python 2.7中的旧式和新式类 [英] Old-style and new-style classes in Python 2.7
问题描述
可能重复:
Python中的旧样式和新样式类
Python 2.7中新样式和旧样式类的当前状态是什么?
What is the current state of affairs with new-style and old-style classes in Python 2.7?
我不经常使用Python,但是我隐约记得这个问题。该文档似乎根本没有提到这个问题: Python教程:类一个>。我还需要为此担心吗?通常,我应该像下面这样声明我的课程吗?
I don't work with Python often, but I vaguely remember the issue. The documentation doesn't seem to mention the issue at all: The Python Tutorial: Classes. Do I still need to worry about this? In general, should I declare my classes like the following?
class MyClass:
pass
还是?
class MyClass(object):
pass
推荐答案
始终子类对象。这些是新样式类。
Always subclass "object". Those are new style classes.
-
您已经准备好使用Python 3。
You are ready for Python 3 that way.
.super()
之类的东西可以正常工作,如果需要的话。
Things like .super()
work properly that way, should you need them.
这篇关于Python 2.7中的旧式和新式类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!