Overloading assignment operator
Erik Max Francis
max at alcyone.com
Tue Jan 23 15:02:40 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?
> All I'm interested in is a clean syntax to script my app. Any ideas are
> very welcome.
Are you sure you even need to do that?
>>> class C:
... def __init__(self, x):
... self.x = x
... def __mul__(self, other):
... return C(self.x*other.x)
...
>>> result = C(2)*C(3)
>>> print result
<__main__.C instance at 0x402e13ec>
>>> result.x
6
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
Life is something to do when you can't get to sleep.
-- Fran Lebowitz
More information about the Python-list
mailing list