Circular Class Logic

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Mar 15 01:38:04 EDT 2007


 <half.italian <at> gmail.com> writes:

> That's not exactly what I'm doing, but your comment still might help.
> I actually want to include an instance of a subclass in it's
> superclass like this:

Move the initialization to another function.

> 
> ===== foo.py =====
> # import Baz
Remove the line above
> 
> class Foo:
> 	# baz = Baz.Baz()
Remove the line above
> 
> 	def __init__(self):
> 		pass
> =====

and add this below:
def initFoo():
  import baz
  Foo.baz = baz.Baz()
initFoo()

No changes in bar.py, baz.py
Instead of initFoo, you could use a custom metaclass. Or a class decorator (if 
and when they become available...)
The code above is an effective way of doing what you want. But I'd think about 
the actual need of doing such things - are you sure it's a good design?

-- 
Gabriel Genellina




More information about the Python-list mailing list