Overloading =

Michael Hudson mwh21 at cam.ac.uk
Thu Feb 17 18:23:19 EST 2000


jhefferon at my-deja.com writes:

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

No. Assignment rebinds references, rather than affecting objects. This
is quite a difference from C++ (say), probably one of the harder ones
to get one's head around.

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

Hmm... do you know about the "exec <blah> in globals,locals" style of
doing things? Though `exec'ing user input sounds deeply unwise to me.
What is it you are trying to do? Maybe we can come up with a better
solution (no promises though).

Cheers,
M.



More information about the Python-list mailing list