Classes referencing each other
Manuel Bleichner
manuel at prolink.de
Fri Sep 1 05:56:07 EDT 2006
Thanks for your answer :)
> You could use a function:
>
> class A(object):
> @staticmethod
> def connected_to(): return [B, C]
> <other attributes...>
I already thought about such a solution, but since
my code has to be compatible with python 2.3, i would
have to use the connected_to = staticmethod(connected_to)
syntax, which bloats the code and makes it quite unreadable.
> 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 ]
This solution seems good to me. It won't work for me as it is,
because the structure of the classes is actually a bit more
complex, but it pushed me in the right direction.
Thanks again,
Manuel
More information about the Python-list
mailing list