在python 3.x中继承python对象是必要的还是有用的?

时间:2022-09-24 23:37:30

In older python version when you create a class in python, it can inherit from object which is as far I understand a special built-in python element that allow your object to be a new-style object.

在旧的python版本中,当您在python中创建类时,它可以从对象继承,据我所知,对象是一个特殊的内置python元素,它允许您的对象是一个新样式的对象。

What about newer version (> 3.0 and 2.6)? I googled about the class object but I get so much result (for an obvious reasons). Any hint?

更新版本(> 3.0和2.6)怎么样?我搜索了类对象,但是得到了很多结果(原因很明显)。有提示吗?

Thank!

谢谢!

2 个解决方案

#1


41  

You don't need to inherit from object to have new style in python 3. All classes are new-style.

在python 3中,不需要从对象继承新样式。所有的类都是新型。

#2


26  

I realise that this is an old question, but it is worth noting that even in python 3 these two things are not quite the same thing.

我意识到这是一个老问题,但值得注意的是,即使在python 3中,这两件事也不是完全相同的。

If you explicitly inherit from object, what you are actually doing is inheriting from builtins.object regardless of what that points to at the time.

如果显式地从对象继承,那么实际上所做的就是从内建项继承。对象,不管它在什么时候指向什么。

Therefore, I could have some (very wacky) module which overrides object for some reason. We'll call this first module "newobj.py":

因此,我可以有一些(非常古怪的)模块,出于某种原因覆盖对象。我们将第一个模块命名为newobject .py:

import builtins

old_object = builtins.object  # otherwise cyclic dependencies

class new_object(old_object):

    def __init__(self, *args, **kwargs):
        super(new_object, self).__init__(*args, **kwargs)
        self.greeting = "Hello World!" 

builtins.object = new_object  #overrides the default object

Then in some other file ("klasses.py"):

然后在其他文件中(“klasses.py”):

class Greeter(object):
    pass

class NonGreeter:
    pass

Then in a third file (which we can actually run):

然后在第三个文件中(我们可以实际运行):

import newobj, klasses  # This order matters!

greeter = klasses.Greeter()
print(greeter.greeting)  # prints the greeting in the new __init__

non_greeter = klasses.NonGreeter()
print(non_greeter.greeting) # throws an attribute error

So you can see that, in the case where it is explicitly inheriting from object, we get a different behaviour than where you allow the implicit inheritance.

可以看到,在显式继承对象的情况下,我们得到的行为与允许隐式继承的行为不同。

#1


41  

You don't need to inherit from object to have new style in python 3. All classes are new-style.

在python 3中,不需要从对象继承新样式。所有的类都是新型。

#2


26  

I realise that this is an old question, but it is worth noting that even in python 3 these two things are not quite the same thing.

我意识到这是一个老问题,但值得注意的是,即使在python 3中,这两件事也不是完全相同的。

If you explicitly inherit from object, what you are actually doing is inheriting from builtins.object regardless of what that points to at the time.

如果显式地从对象继承,那么实际上所做的就是从内建项继承。对象,不管它在什么时候指向什么。

Therefore, I could have some (very wacky) module which overrides object for some reason. We'll call this first module "newobj.py":

因此,我可以有一些(非常古怪的)模块,出于某种原因覆盖对象。我们将第一个模块命名为newobject .py:

import builtins

old_object = builtins.object  # otherwise cyclic dependencies

class new_object(old_object):

    def __init__(self, *args, **kwargs):
        super(new_object, self).__init__(*args, **kwargs)
        self.greeting = "Hello World!" 

builtins.object = new_object  #overrides the default object

Then in some other file ("klasses.py"):

然后在其他文件中(“klasses.py”):

class Greeter(object):
    pass

class NonGreeter:
    pass

Then in a third file (which we can actually run):

然后在第三个文件中(我们可以实际运行):

import newobj, klasses  # This order matters!

greeter = klasses.Greeter()
print(greeter.greeting)  # prints the greeting in the new __init__

non_greeter = klasses.NonGreeter()
print(non_greeter.greeting) # throws an attribute error

So you can see that, in the case where it is explicitly inheriting from object, we get a different behaviour than where you allow the implicit inheritance.

可以看到,在显式继承对象的情况下,我们得到的行为与允许隐式继承的行为不同。