Reference class in class creation

Gregor Horvath gh at gregor-horvath.com
Tue Nov 21 07:24:23 EST 2006


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