Reference class in class creation

Diez B. Roggisch deets at nospam.web.de
Tue Nov 21 07:37:11 EST 2006


Gregor Horvath wrote:

> Hi,
> 
> I want to reference a class itself in its body:
> 
> class SomeElement(object):
> def __init__(self, mycontainer):
> self.mycontainer=mycontainer
> 
> class SomeContainer(object):
> a = SomeElement(SomeContainer)
> 
> 
> Unfortunatly this does not work since the name SomeContainer is not
> definied at class creation time of SomeContainer:
> 
> /tmp/python-9309vMe.py in SomeContainer()
>       4
>       5 class SomeContainer(object):
> ----> 6       a = SomeElement(SomeContainer)
>       7
>       8
> 
> NameError: name 'SomeContainer' is not defined
> 
> 
> How to do this?

Anything wrong with:

class Foo(object):
   pass

Foo.a = Foo

?

Diez




More information about the Python-list mailing list