Looking for assignement operator

Jerry jweida at gmail.com
Tue Oct 17 16:26:39 CEST 2006



On Oct 17, 8:50 am, Alexander Eisenhuth <newsu... at stacom-software.de>
wrote:
> Hello,
>
> is there a assignement operator, that i can overwrite?
>
> class MyInt:
>         def __init__(self, val):
>                 assert(isinstance(val, int))
>                 self._val = val
>
> a = MyInt(10)
>
> # Here i need to overwrite the assignement operator
> a = 12
>
> Thanks
> Alexander

I believe the property function is what you are looking for.  e.g.

class MyClass:
    def __init__(self, val):
        self.setval(val)

    def getval(self):
        return self._val

    def setval(self, val):
        assert(isinstance(val, int))
        self._val = val

    _val = property(self.getval, self.setval)

--
Jerry




More information about the Python-list mailing list