Overloading assignment operator

Paul McGuire ptmcg at austin.rr.com
Tue Jan 23 14:19:01 EST 2007


On Jan 23, 12:24 pm, Achim Domma <d... at procoders.net> wrote:
> Hi,
>
> I want to use Python to script some formulas in my application. The user
> should be able to write something like
>
> A = B * C
>
> where A,B,C are instances of some wrapper classes. Overloading * is no
> problem but I cannot overload the assignment of A. I understand that
> this is due to the nature of Python, but is there a trick to work around
> this?
> All I'm interested in is a clean syntax to script my app. Any ideas are
> very welcome.
>
> regards,
> Achim

Simple option: how do you feel about using '<<=' instead of '=' (by
defining __ilshift__)?  This gives you:

A <<= B * C

(looks sort of like "injecting" the result of B times C into A)

More complicated option: embed an expression/assignment parser into
your app.  You can get a jump on this using some of the examples that
come with pyparsing (can check these out online at
http://pyparsing.wikispaces.com/Examples - look at fourFn.py and
simpleArith.py).

-- Paul




More information about the Python-list mailing list