简体   繁体   English

如何从父项获取属性?

[英]How to get attributes from parent?

Let's say we have these classes: 假设我们有以下类:

class Foo(object):
    _bar = ""

    def __init__(self):
        self.bar = "hello"

    def getBar(self):
        return self._bar

    def setBar(self, bar):
        self._bar = bar

    def getAttributes(self):
        for attr in self.__dict__:
            print attr

    bar = property(getBar, setBar)

class Child(Foo):

    def __init__(self):
        super(Child, self).__init__()
        self.a = ""
        self.b = ""

if I do something like: 如果我这样做:

child = Child()
child.getAttributes()

I get all the attributes from parent and child. 我从父母和孩子那里获得了所有属性。 How could I get the attributes only from the parent? 如何仅从父级获取属性?

Thanks in advance! 提前致谢!

You can't. 你不能。 Python stores all object attributes in the same place "on the object"; Python将所有对象属性存储在“对象上”的同一位置; it does not break them out into separate storage areas, one per class in the inheritance hierarchy. 它不会将它们分成单独的存储区域,在继承层次结构中每个类一个。 The best you could do would be to keep a list of attributes that belonged to each level, maybe by having a __slots__ = [] declaration in every class or maybe by keeping a less formal list of attribute names, and then doing a loop with getattr() inside (or a loop that looks directly in the object's __dict__ ?) to get that specific set of attributes. 最好的办法是保留一个属于每个级别的属性的列表,可能是在每个类中都有一个__slots__ = []声明,或者可能是保留一个不太正式的属性名称列表,然后使用getattr()循环getattr()内部(或直接在对象的__dict__ ?中查找的循环)以获取特定的属性集。

What are you trying to do with this list of attributes? 您要使用此属性列表做什么? What do you want to accomplish with them? 您想与他们达成什么? Maybe there is a better way to tackle your larger issue. 也许有更好的方法来解决更大的问题。

"How could I get the attributes only from the parent?" “我怎么只能从父母那里得到属性?”

There is no "parent", really. 确实没有“父母”。 Child(Foo) is a class, so Foo is the "parent" here (although you usually call it "superclass"), but that relationship doesn't carry over to the instances. Child(Foo)是一个类,因此Foo在这里是“父”(尽管通常将其称为“超类”),但是这种关系不会延续到实例中。

When you instantiate the class by child = Child() , child has no parent. 当您通过child = Child()实例化类时, child没有父级。 All it's attributes sit directly on the instance, so there is no parent, really. 它的所有属性都直接位于实例上,因此实际上没有父级。 And that's why it becomes problematic. 这就是为什么它会成为问题。 But as Brandon suggests, by using slots, or otherwise defining up which properties belong to which superclass, you can return only those defined in a particular class. 但是正如布兰登建议的那样,通过使用插槽或以其他方式定义哪些属性属于哪个超类,您只能返回在特定类中定义的那些属性。

Why on earth you would want this is beyond me, though. 但是,为什么在地球上您会希望这超出了我的范围。 :) :)

Quick example (which may, or not be what you want): 快速示例(可能不是您想要的):

child.__class__.__bases__[0]().getAttributes()

Edit : to clarify things a bit: __base__ is a class attribute containing tuple of base classes for this class. 编辑 :稍微澄清一下: __base__是一个类属性,包含该类的基类的元组。 Since your Child class has only one base class Foo , i'm just selecting first element of this tuple. 由于您的Child类只有一个基类Foo ,因此我只是选择该元组的第一个元素。 Using your data as an input, it will produce: 使用您的数据作为输入,它将产生:

>>> child.getAttributes()
a
_bar
b

>>> child.__class__.__bases__[0]().getAttributes()
_bar

If you change the getAttributes() method in class Foo to: 如果将class FoogetAttributes()方法更改为:

def getAttributes(self):
    for attr in self.__class__.__bases__[0].__dict__:
        print attr

You'll then get the following results from calling it on a Child instance: 然后,通过在Child实例上调用它会得到以下结果:

child = Child()
child.getAttributes()
# setBar
# __module__
# bar
# getAttributes
# __dict__
# __doc__
# __weakref__
# _bar
# __init__
# getBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM