Reputation: 15788
With a boring class, object instance attribute shadow class attributes:
class C(object):
a="class_a"
def __init__(self, a):
self.a = a
c = C(a="obja")
print c.a # obja
But if my class attributes are declared in a named_tuple base:
class C(collections.namedtuple("CBase", ['a', ])):
a="class_a"
c = C(a="obja")
print c.a # class_a !!??!
... so, declaring my instance attribute through the name tuple causes that attribute to be shadowed by the class attribute ... which not what you'd expect.
Why is this?
Upvotes: 0
Views: 163
Reputation: 155505
namedtuple
"attributes" are implemented as descriptors (specifically, property
s) on the class itself, not attributes in the traditional sense (all the actual data is stored in unnamed indices of the tuple
). In this case, the namedtuple
(roughly) defines:
@property
def a(self):
return self[0]
Since the property is a class level attribute, when you define a
on the subclass, it shadows equivalent definitions in the parent class.
Upvotes: 3