Overloading assignment operator

Peter Otten __peter__ at web.de
Tue Jan 23 13:42:01 EST 2007


Achim Domma wrote:

> 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?

>>> class D(dict):
...     def __setitem__(self, key, value):
...             print key, "<--", value
...             dict.__setitem__(self, key, value)
...
>>> namespace = D(B=42, C=24)
>>> exec "A = B * C" in namespace
A <-- 1008

Peter



More information about the Python-list mailing list