所有 Python 类都应该扩展对象吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15374857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should all Python classes extend object?
提问by Kara
I have found that both of the following work:
我发现以下两项都有效:
class Foo():
def a(self):
print "hello"
class Foo(object):
def a(self):
print "hello"
Should all Python classes extend object? Are there any potential problems with not extending object?
所有 Python 类都应该扩展对象吗?不扩展对象是否有任何潜在问题?
采纳答案by Fred Foo
In Python 2, not inheriting from objectwill create an old-style class, which, amongst other effects, causes typeto give different results:
在 Python 2 中,不继承自object将创建一个旧式类,除其他影响外,还会导致type给出不同的结果:
>>> class Foo: pass
...
>>> type(Foo())
<type 'instance'>
vs.
对比
>>> class Bar(object): pass
...
>>> type(Bar())
<class '__main__.Bar'>
Also the rules for multiple inheritance are differentin ways that I won't even try to summarize here. All good documentation that I've seen about MI describes new-style classes.
多重继承的规则也有所不同,我什至不会在这里总结。我见过的所有关于 MI 的优秀文档都描述了新式类。
Finally, old-style classes have disappeared in Python 3, and inheritance from objecthas become implicit. So, always prefer new style classes unless you need backward compat with old software.
最后,旧式类在 Python 3 中消失了,继承自object变得隐式。所以,除非你需要向后兼容旧软件,否则总是喜欢新风格的类。
回答by pydsigner
Yes, all Python classes should extend (or rather subclass, this is Python here) object. While normally no serious problems will occur, in some cases (as with multiple inheritance trees) this will be important. This also ensures better compatibility with Python 3.
是的,所有 Python 类都应该扩展(或者更确切地说是子类,这里是 Python)对象。虽然通常不会发生严重的问题,但在某些情况下(如多继承树)这很重要。这也确保了与 Python 3 更好的兼容性。
回答by slezica
In Python 3, classes extend objectimplicitly, whether you say so yourself or not.
在 Python 3 中,类object隐式扩展,无论您自己是否这么说。
In Python 2, there's old-style and new-styleclasses. To signal a class is new-style, you have to inherit explicitly from object. If not, the old-style implementation is used.
在 Python 2 中,有旧式和新式类。要表示类是新样式,您必须显式继承自object. 如果不是,则使用旧式实现。
You generally want a new-style class. Inherit from objectexplicitly. Note that this also applies to Python 3 code that aims to be compatible with Python 2.
您通常需要一个新型的类。从object显式继承。请注意,这也适用于旨在与 Python 2 兼容的 Python 3 代码。
回答by thkang
in python3 there isn't a differance, but in python2 not extending objectgives you an old-style classes; you'd like to use a new-style class over an old-style class.
在 python3 中没有区别,但在 python2 中不扩展object给你一个旧式的类;您想在旧式类上使用新式类。
回答by N Randhawa
In python 3 you can create a class in three different ways & internally they are all equal (see examples). It doesn't matter how you create a class, all classes in python 3 inherits from special class called object. The class objectis fundamental class in python and provides lot of functionality like double-underscore methods, descriptors, super() method, property() method etc.
在 python 3 中,您可以通过三种不同的方式创建一个类,并且在内部它们都是相等的(参见示例)。无论您如何创建类,python 3 中的所有类都继承自称为object 的特殊类 。类对象是python中的基础类,提供了许多功能,如双下划线方法、描述符、super()方法、property()方法等。
Example 1.
示例 1。
class MyClass:
pass
Example 2.
示例 2。
class MyClass():
pass
Example 3.
例 3。
class MyClass(object):
pass
回答by jmoz
As other answers have covered, Python 3 inheritance from object is implicit. But they do not state what you should do and what is convention.
正如其他答案所涵盖的那样,Python 3 从对象继承是隐式的。但是他们没有说明您应该做什么以及什么是惯例。
The Python 3 documentation examples all use the following style which is convention, so I suggest you follow this for any future code in Python 3.
Python 3 文档示例都使用以下约定样式,因此我建议您在 Python 3 中的任何未来代码中都遵循此样式。
class Foo:
pass
Source: https://docs.python.org/3/tutorial/classes.html#class-objects
来源:https: //docs.python.org/3/tutorial/classes.html#class-objects
Example quote:
示例报价:
Class objects support two kinds of operations: attribute references and instantiation.
Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class's namespace when the class object was created. So, if the class definition looked like this:
class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world'
类对象支持两种操作:属性引用和实例化。
属性引用使用 Python 中用于所有属性引用的标准语法:obj.name。有效的属性名称是创建类对象时在类的命名空间中的所有名称。因此,如果类定义如下所示:
class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world'
Another quote:
另一个引用:
Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:
class Dog: kind = 'canine' # class variable shared by all instances def __init__(self, name): self.name = name # instance variable unique to each instance
一般来说,实例变量用于每个实例唯一的数据,类变量用于类的所有实例共享的属性和方法:
class Dog: kind = 'canine' # class variable shared by all instances def __init__(self, name): self.name = name # instance variable unique to each instance

