On 9/20/06, Alan Gauld <[EMAIL PROTECTED]> 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.
as others have mentioned, the assignment operator is used to assign an object to a name in the current namespace... IOW, you cannot "overload the assignment operator." __coerce__() is used for arithmetic operations, so this won't work either. what you really want to do is to allow (validated) access to mydie.value (e.g., self.value). instead of "mydie = 5" -- which is taking the 'mydie' name and reassigning it to the integer object that has a value of 5 (thus losing the reference to your instance object, decrementing its reference count, etc.), you want to allow the user to do something like: mydie.value = 5 ... BUT, you want that value to be validated before it actually assigns it to the mydie.value instance attribute. here is where properties become useful. you can create a getter, setter, and even a deleter and doc string if you want. here's how you use it... add the following to your class: def get_value(self): return self.__value # pretty much your set() method def set_value(self, newval): assert isinstance(newval, int), 'must be an int!' assert 1 <= newval <= self.n, 'invalid value!' self.__value = newval value = property(get_value, set_value, doc='value of mydie') ----- in actuality, the value is stored in self.__value, but access is via self.value. this should give you what you need provided you are happy with using "mydie.value = ..." vs. "mydie = ...", the latter of which will never work the way you want. with the addition of the above code, you can leave __init__() and roll() alone as the self.value = ... assignment will still call your property methods to do the assigning. (also note that roll() does not have to return self.value unless that is desired.) now, even with properties, the bad news is that someone can be sneaky and do something like "mydie.set_value(200)" to try and get around doing "mydie.value = 200". in other words, you cannot restrict access to the property methods. the good news is that there is a workaround to this. i have an example in one of the newly-written sections in (the 2nd ed of) my book that was inspired by the following cookbook recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/205183 the crux of this recipe is that it is very much like using a closure to get your cake and eat it too. you stick your getter (and perhaps setter) into another scope which is then rendered inaccessible to the instance. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor