[Tutor] Overloading the assignment operator in a class

Alan Gauld alan.gauld at btinternet.com
Wed Sep 20 23:15:11 CEST 2006


> This all seems to work okay.  
>
> I want the assignment operator ('=') 

There is no assignment operator in Python, assignment is a 
binding of an object to a name.

> to call the 
> set method transparently on Die instances, 
> as in this fictitious example:

@BCARROLL[Python]|6> mydie = 5
@BCARROLL[Python]|7> mydie
                 <7> 5

But you can fake this by coercing the integer into a new 
Die object. As if you had actually done


mydie = Die(mydie.n,5)

And I believe you can do that by implementing the __coerce__ 
method. - but I've never tried it...

HTH,

Alan G.





More information about the Tutor mailing list