Python:如何通过派生类实例访问父类对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2265060/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:12:55  来源:igfitidea点击:

Python: How to access parent class object through derived class instance?

python

提问by PyNewbie

I'm sorry for my silly question, but... let's suppose I have these classes:

我很抱歉我的愚蠢问题,但是......让我们假设我有这些课程:

class A():
    msg = 'hehehe'

class B(A):
    msg = 'hohoho'

class C(B):
    pass

and an instance of B or C. How do I get the variable 'msg' from the parent's class object through this instance? I've tried this:

和 B 或 C 的实例。如何通过此实例从父类对象中获取变量“msg”?我试过这个:

foo = B()
print super(foo.__class__).msg

but got the message: "TypeError: super() argument 1 must be type, not classobj".

但收到消息:“类型错误:super() 参数 1 必须是类型,而不是 classobj”。

回答by Tim Dumol

You actually want to use

你实际上想使用

class A(object):
    ...
...
b = B()
bar = super(b.__class__, b)
print bar.msg

Base classes must be new-style classes (inherit from object)

基类必须是新式类(继承自object

回答by kennytm

If the class is single-inherited:

如果类是单继承的:

foo = B()
print foo.__class__.__bases__[0].msg
# 'hehehe'

If the class is multiple-inherited, the question makes no sense because there may be multiple classes defining the 'msg', and they could all be meaningful. You'd better provide the actual parent (i.e. A.msg). Alternatively you could iterate through all direct bases as described in @Felix's answer.

如果类是多重继承的,这个问题就没有意义,因为可能有多个类定义了“msg”,而且它们都可能是有意义的。你最好提供实际的父级(即A.msg)。或者,您可以按照@Felix的回答中的描述遍历所有直接基础。

回答by John La Rooy

Not sure why you want to do this

不知道你为什么要这样做

>>> class A(object):
...     msg = 'hehehe'
... 
>>> class B(A):
...     msg = 'hohoho'
... 
>>> foo=B()
>>> foo.__class__.__mro__[1].msg
'hehehe'
>>> 

回答by Olivier Verdier

Try with:

尝试:

class A(object):
    msg = 'hehehe'

EDIT:

编辑:

For the 'msg' attribute you would need:

对于 'msg' 属性,您需要:

foo = B()
bar = super(foo.__class__, foo)
print bar.msg

回答by Felix Kling

As msgis a class variable, you can just do:

作为msg类变量,您可以这样做:

print C.msg    # prints hohoho

If you overwrite the variable (as you do in class B), you have to find the right parent class. Remember that Python supports multiple inheritance.

如果覆盖变量(就像在 class 中所做的那样B),则必须找到正确的父类。请记住,Python 支持多重继承

But as you define the classes and you now that Binherits from Ayou can always do this:

但是当您定义类并且B继承自A您的类时,您始终可以这样做:

class B(A):
    msg = 'hohoho'

    def get_parent_message(self):
       return A.msg

UPDATE:

更新:

The most reliable thing would be:

最靠谱的应该是:

def get_parent_attribute(instance, attribute):
    for parent in instance.__class__.__bases__:
        if attribute in parent.__dict__:
             return parent.__dict__[attribute]

and then:

接着:

foo = B()
print get_parent_attribute(foo, 'msg')

回答by Amarghosh

#for B() you can use __bases__
print foo.__class__.__bases__[0].msg

But this is not gonna be easy when there are multiple base classes and/or the depth of hierarchy is not one.

但是当有多个基类和/或层次结构的深度不是一个时,这并不容易。