In an inherited class, "embedded" classes is referenced?

Christian Joergensen mail at razor.dk
Wed Dec 19 16:23:42 EST 2007


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:

>>> B.C = type("C", (object,), {'foobar': 60})

Instead of:

>>> B.C.foobar = 60

Thanks,

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  |     Visit us at: http://www.gmta.info



More information about the Python-list mailing list