所有Python类都应该扩展对象吗?

问题:所有Python类都应该扩展对象吗?

我发现以下两项工作:

class Foo():
    def a(self):
        print "hello"

class Foo(object):
    def a(self):
        print "hello"

所有Python类都应该扩展对象吗?不扩展对象是否存在任何潜在问题?

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?


回答 0

在Python 2中,不继承自object将创建一个旧式类,除其他效果外,该类还会导致type产生不同的结果:

>>> class Foo: pass
... 
>>> type(Foo())
<type 'instance'>

>>> class Bar(object): pass
... 
>>> type(Bar())
<class '__main__.Bar'>

多重继承的规则也不同在这里我什至不尝试总结。我所见过的有关MI的所有好的文档都描述了新型类。

最终,旧式类在Python 3中消失了,并且继承自object隐式了。因此,除非您需要与旧软件向后兼容,否则请始终偏爱新样式类。

In Python 2, not inheriting from object will create an old-style class, which, amongst other effects, causes type to give different results:

>>> class Foo: pass
... 
>>> type(Foo())
<type 'instance'>

vs.

>>> class Bar(object): pass
... 
>>> type(Bar())
<class '__main__.Bar'>

Also the rules for multiple inheritance are different in ways that I won’t even try to summarize here. All good documentation that I’ve seen about MI describes new-style classes.

Finally, old-style classes have disappeared in Python 3, and inheritance from object has become implicit. So, always prefer new style classes unless you need backward compat with old software.


回答 1

在Python 3中,object无论您是否自己说,类都隐式扩展。

在Python 2中,有旧式和新式类。要发出新的类信号,您必须显式继承自object。如果不是,则使用旧式实现。

通常,您需要一个新式的类。object显式继承。请注意,这也适用于旨在与Python 2兼容的Python 3代码。

In Python 3, classes extend object implicitly, whether you say so yourself or not.

In Python 2, there’s old-style and new-style classes. To signal a class is new-style, you have to inherit explicitly from object. If not, the old-style implementation is used.

You generally want a new-style class. Inherit from object explicitly. Note that this also applies to Python 3 code that aims to be compatible with Python 2.


回答 2

在python 3中,您可以通过三种不同的方式创建一个类,并且在内部它们都相等(请参见示例)。创建类无关紧要,Python 3中的所有类都继承自称为object的特殊类 。类对象 是python中的基础类,并提供许多功能,例如双下划线方法,描述符,super()方法,property()方法等。

范例1。

class MyClass:
 pass

示例2

class MyClass():
 pass

范例3。

class MyClass(object):
  pass

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 object is fundamental class in python and provides lot of functionality like double-underscore methods, descriptors, super() method, property() method etc.

Example 1.

class MyClass:
 pass

Example 2.

class MyClass():
 pass

Example 3.

class MyClass(object):
  pass

回答 3

是的,所有Python类都应扩展(或更确切地说是子类,这里是Python)对象。尽管通常不会发生严重的问题,但在某些情况下(如具有多个继承树),这将很重要。这也确保了与Python 3的更好兼容性。

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.


回答 4

正如其他答案所涵盖的那样,从对象继承Python 3是隐式的。但是他们没有说明您应该做什么以及什么是惯例。

Python 3文档示例全部使用以下约定的样式,因此,我建议您在以后使用Python 3的任何代码中都遵循此样式。

class Foo:
    pass

资料来源:https : //docs.python.org/3/tutorial/classes.html#class-objects

引用示例:

类对象支持两种操作:属性引用和实例化。

属性引用使用Python中所有属性引用使用的标准语法:obj.name。有效属性名称是创建类对象时在类命名空间中的所有名称。因此,如果类定义如下所示:

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

另一句话:

一般来说,实例变量用于每个实例唯一的数据,类变量用于类的所有实例共享的属性和方法:

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

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.

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.

class Foo:
    pass

Source: 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'

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

回答 5

在python3中没有区别,但是在python2中不扩展object给您带来了老式的类;您想在旧类上使用新类。

in python3 there isn’t a differance, but in python2 not extending object gives you an old-style classes; you’d like to use a new-style class over an old-style class.