Is this type of forward referencing possible?

Paul McGuire ptmcg at austin.rr.com
Sun Mar 15 16:33:32 CET 2009


On Mar 15, 6:33 am, "andrew cooke" <and... at acooke.org> wrote:
> someone else has answered this, but an extra trick that is sometimes
> useful is that while there is no forward referencing you can often exploit
> late binding and evaluation order.
>
> your example will not work because code at the class level is evaluated
> when the module is loaded.  but code at the method level is only evaluated
> when called, so
>
> class A:
>   def __init__(self):
>     self.someType = B
>
> class B:
>   anotherType = A
>
> will work.
>
> andrew
>

Not the same.  The OP wanted someType to be a class-level attribute,
not an instance level one.

I'm guessing the class mapping is all part of an overall design, so I
would define all of these after creating A and B as empty classes:

class A: pass
class B: pass

A.someType = B
B.anotherType = A

-- Paul



More information about the Python-list mailing list