Overloading assignment operator
Gabriel Genellina
gagsl-py at yahoo.com.ar
Wed Jan 24 00:23:41 EST 2007
"Carroll, Barry" <Barry.Carroll at psc.com> escribió en el mensaje
news:2BBAEE949D384D40A2B851287ADB6A4304595AA4 at eugsrv400.psc.pscnet.com...
> * ... the assignment operator is used to assign an
> * object to a name in the current namespace... IOW, you cannot "overload
> * the assignment operator."
> (wesley chun)
This is the key concept, if you want to be able to "see" assignments, you
should work with a namespace (a dictionary is enough...)
> * result = property(get_result, set_ result, doc='result of operations')
>
> I have tested this using the admittedly simple-minded code snipped
> below.
>
>>>>>>>>>>>
> @BCARROLL[Python]|3> class Aclass:
> |.> def __init__(self):
> |.> __result = None
> |.> def get_result(self):
> |.> return self.__result
> |.> def set_result (self, result):
> |.> self.__result = result
> |.> result = property(get_result, set_result,
> doc='result of expression')
> |.>
> @BCARROLL[Python]|5> A = Aclass()
> @BCARROLL[Python]|7> a.result = 2*3
> @BCARROLL[Python]|8> a.result
> <8> 6
> @BCARROLL[Python]|9> a.result = 25.0 * 5.25
> @BCARROLL[Python]|10> a.result
> <10> 131.25
> @BCARROLL[Python]|11>
>>>>>>>>>>>
Notice that you can delete ALL those lines of code, leaving a bare:
class Aclass: passs
and you will get EXACTLY the same results. In this case -and this are not my
words- properties are a waste of programmer time, processor time and disk
space.
Of course properties are very useful, but when you need to do something
"more" than just storing and retrieving a single value.
--
Gabriel Genellina
More information about the Python-list
mailing list