Circular Class Logic

half.italian at gmail.com half.italian at gmail.com
Wed Mar 14 21:19:53 EDT 2007


>
> That is, each of the classes want to inherit from the others.

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:

===== foo.py =====
import Baz

class Foo:
	baz = Baz.Baz()

	def __init__(self):
		pass
=====

===== bar.py =====
import Foo

class Bar(Foo.Foo):
	pass
=====

===== baz.py =====
import Bar

class Baz(Bar.Bar):
	pass
=====

> The usual solution in these cases is to find the common required
> functionality and factor that out to a separate class that is then the
> superclass of two of the existing classes, breaking the circle.
>
> ===== wibble.py =====
> # no dependencies
>
> class Wibble(object):
>     pass
> =====
>
> ===== foo.py =====
> import wibble
> import bar
>
> class Foo(wibble.Wibble, bar.Bar):
>     pass
> =====
>
> ===== baz.py =====
> import wibble
>
> class Baz(wibble.Wibble):
>     pass
> =====
>
> Note that Baz no longer subclasses foo.Foo, and both Foo and Baz get
> the common functionality they share from wibble.Wibble.

I have to think about that for a bit and see if it makes sense to
factor anything out.  Thanks for the idea.  Would that be the best
solution considering the above description?

~Sean
>
> --
>  \     "Buy not what you want, but what you need; what you do not need |
>   `\           is expensive at a penny."  -- Cato, 234-149 BC, Relique |
> _o__)                                                                  |
> Ben Finney





More information about the Python-list mailing list