简体   繁体   中英

How to reference a base class' attribute in derived class?

I can do this:

class Blah2:
    atttr = 5
    aa = atttr


ob = Blah2
print(ob.aa)

http://ideone.com/pKxMc2

So I assumed I could as well do this:

class Blah1:
    atttr = 5


class Blah2(Blah1):
    aa = atttr


ob = Blah2
print(ob.aa)

Nope I can't: http://ideone.com/6HS1MO

SPits out the following error:

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
  File "./prog.py", line 6, in Blah2
NameError: name 'atttr' is not defined

Why doesn't this work and how to make it working?

The class block scope only exists temporarily during class definition. After class definition, you would have to access that attribute via the class object, ie Blah1.atttr .

This is documented under the execution model section.

Class definition blocks and arguments to exec() and eval() are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block ; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope.

Your problem is fixed like this:

class Blah1:
    atttr = 5


class Blah2(Blah1):
    aa = BLah1.atttr

However, as pointed out in the comments, this is a weird thing to do. Now you have this:

>>> ob = Blah2()
>>> assert ob.aa == ob.atttr
True

EDIT: As stated in wim's answer, the cause of this is how scoping works inside of a class definition. When you do this:

class C():
    a = 1

It is pretty much equivalent to this:

C = type('C', (), {}) # <--- dynamic class creation function
C.a = 1

As you can see, in order to access that a anywhere outside of the class definition body, you have to refer to it using the class (or an instance of the class): Ca or c().a . But note this "gotcha":

class C():
    a = 1

c = C()
c.a = 2
print(C.a) #  prints 1!!!

It prints 1 because after the second line above, there is now both a c attribute a and a C attribute a , and they are two different objects:

>>> vars(c)
{'a': 2}
>>> vars(C)
{'a': 1}

If you want ca to not override Ca , you need to learn about properties and descriptors. Maybe start here .

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