Reference class in class creation
stephen04 at tiscali.co.uk
stephen04 at tiscali.co.uk
Tue Nov 21 13:31:13 EST 2006
class Foo(object):
me = None
def __init__(self):
Foo.me = self
easy!
"Gregor Horvath" <gh at gregor-horvath.com> wrote in message
news:a1b59$4562eff8$547078de$21737 at news.chello.at...
> 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?
>
> Unfortunatly the obvious:
>
> class SomeContainer(object):
> def __init__(self):
> self.a = SomeElement(SomeContainer)
>
> is not possible because of other constraints.
>
> --
> Servus, Gregor
>
More information about the Python-list
mailing list