Instance of inherited nested class in outer class not allowed?

Diez B. Roggisch deets at
Wed Feb 27 23:15:59 CET 2008

mrstephengross schrieb:
>> class Foo:
>>       foo = Foo()
>> You have to live with that. Just do
>> = Outer.Parent()
>> after your class-statement to achieve the same result.
> Hmmm. Well, I see why that works. It's too bad, though. If I want to
> keep all executed code safely within a "if __name__ == '__main__'"
> block, it ends up a bit ugly. Then again, I guess this is just an
> aspect of python I'll have to get used to. Is there a specific reason
> it works this way, by chance?

Well, what would you think python should make of this?

class Foo:

     f = Foo()

     def __init__(self, argument):

It can't possibly allow to instantiate an object of a class unless the 
class creation is finished. Of course it could delay the execution of 
anything but method definitions. But then the price would be high - loss 
of generatlity, and for example this weren't possible as well:

class Bar:
     if relative_moon_moisture() > 10:
        def foo(self): pass
        def bar(self): pass


More information about the Python-list mailing list