Overloading operators on python properties

Is it possible to overload a statement in a python property? Sort of:

class Foo( object ):

    @property
    def bar( self ): return unfoobar( self._bar )

    @bar.setter
    def bar( self, baz ): self._bar = foobar( baz )

    @bar.__eq__
    def bar( self, baz ): return self._bar == foobar( baz )

without defining a special class for _bar (although in this example this is probably the best solution ...).

+2
source share
1 answer

No, the operators are applied to the attribute value (whether it is set by a property or taken from a map __dict__or slot), the attribute is never itself.

You will have to return a special shell that implements __eq__, and I will return it, I'm afraid.

+1
source

Source: https://habr.com/ru/post/1538035/


All Articles