Alan Gauld wrote: >> 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. >
If you can do that with __coerce__, I'm not clever enough to figure out how. IIRC, Python only calls __coerce__ if you're using arithmetic operators on different types, and only if the operator in question isn't overloaded to handle this case. Ex: In [1]: class coerceTest: ...: def __init__(self, val): ...: self.val=val ...: ...: def __coerce__(self, other): ...: return self.val, other In [2]: test=coerceTest(5) In [3]: test Out[3]: <__main__.coerceTest instance at 0x00E29620> In [4]: result=test+10 In [5]: result Out[5]: 15 In [6]: test=5 In [7]: test Out[7]: 5 (I could've written a test to show that __coerce__ is only called when no __add__ is defined, but I'm lazy and its time to leave work!) -Jordan Greenberg _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor