简体   繁体   中英

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"; 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.

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.

When you instantiate the class by child = Child() , child has no parent. 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. Since your Child class has only one base class Foo , i'm just selecting first element of this tuple. 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:

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.getAttributes()
# setBar
# __module__
# bar
# getAttributes
# __dict__
# __doc__
# __weakref__
# _bar
# __init__
# getBar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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