Classes referencing each other
Duncan Booth
duncan.booth at invalid.invalid
Fri Sep 1 05:12:42 EDT 2006
"Manuel Bleichner" <manuel at prolink.de> wrote:
> If anyone of you knows a neat way to solve this, I'd be
> very grateful.
You could use a function:
class A(object):
@staticmethod
def connected_to(): return [B, C]
<other attributes...>
class B(object)
@staticmethod
def connected_to(): return [C]
<other attributes...>
class C(object)
@staticmethod
def connected_to(): return [A]
<other attributes...>
for cls in globals().values():
if (type(cls) is type and
hasattr(cls, 'connected_to') and
callable(cls.connected_to)):
cls.connected_to = cls.connected_to()
or just store the names of the classes and do a similar fixup once they are
all defined:
class A(object):
connected_to = ['B', 'C']
<other attributes...>
for cls in globals().values():
if (type(cls) is type and
hasattr(cls, 'connected_to')):
cls.connected_to = [globals()[c] for c in cls.connected_to ]
More information about the Python-list
mailing list