Overloading =

Felix Thibault felixt at dicksonstreet.com
Sun Feb 20 20:38:08 CET 2000


At 22:47 2/17/00 GMT, jhefferon at my-deja.com wrote:
>I have a class, rclass.  It has an attribute value.  Can I arrange
>so that the string
>    r1=r2
>sets r1.value to equal r2.value (where r1 and r2 are class instances,
>of course) without changing anything else about r1?  I'm willing to
>test, say, that r1.classtype==r2.classtype or something, first to make
>sure the assignment is sensible.
>
>And if so, can I also have r1=7 set r1.value to be 7?
>
>I want to have a box in a GUI where users can enter code, such as
>assignments, and I'd like to avoid the reference to the underlying
>attribute.
>
>Thanks,
>Jim Hefferon jim at joshua.smcvt.edu
>
>
>Sent via Deja.com http://www.deja.com/
>Before you buy.
>-- 
>http://www.python.org/mailman/listinfo/python-list
>
>

Would it be OK for this assignment to use another operation ?
You could use something like-

def __lshift__(self, other):
    if hasattr(other, 'value'):
        self.value = other.value
    else:
        self.value = other

then your users could type r1 << r2 to put the value of r2 into r1.

-Felix





More information about the Python-list mailing list