What this code should print ?
class Parent():
class Meta(object):
classattr = "Hello"
class Child(Parent):
pass
Child.Meta.classattr = "world"
ch = Child()
pr = Parent()
ch.Meta.classattr = "Oppa"
print Parent.Meta.classattr
print Child.Meta.classattr
print pr.Meta.classattr
print ch.Meta.classattr
I've expected following:
Hello
world
Hello
Oppa
But got
Oppa
Oppa
Oppa
Oppa
So... i can modify parent class (not instance!) by modifying child class instance. Is that normal ?
Child.Meta
actually isParent.Meta
- classChild
has no ownMeta
class attribute so attribute's lookup rules resolve toParent.Meta
. Also since it's a class attribute, any lookup of an instance of ofParent
(including instances ofChild
) will resolve to very same class object.