Christian Joergensen schrieb: > Hello > > I stumpled upon this "feature" during my work tonight, and found it > a bit confusing: > >>>> class A(object): > ... class C: > ... foobar = 42 > ... >>>> class B(A): pass > ... >>>> A.C > <class __main__.C at 0xb7cf735c> >>>> B.C > <class __main__.C at 0xb7cf735c> >>>> B.C.foobar = 60 >>>> A.C.foobar > 60 > > When I inherit B from A, I would expect that A.C and B.C would be two > different classes? But apparently not. > > Can anyone give me an explanation? Or a better workaround than > something along the line of:
Why should they be different? The class-statment of A is only exectuted once, as is the nested class' C. Which makes A.C just a "normal" class-variable. That is of course shared amongst subclasses. As are methods, properties and every other thing living in the A.__dict__ due to the MRO in python. The more important question is: what do you need C for? Do you have by any chance a Java-background and think of C as inner/nested class as in Java? This feature doesn't exist in Python. Your workaround might be implementable using a metaclass in a more conveinient way, but I'm not sure-footed enough with metaclasses to provide a solution out of my head now. Diez -- http://mail.python.org/mailman/listinfo/python-list