2

Is it possible to overload an operator on a python property? Something like:

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, that's probably the best solution...).

4

1 回答 1

1

No, operators are applied to the value of an attribute (be it supplied by a property or taken from the __dict__ mapping or a slot), never the attribute itself.

You will have to return a special wrapper implementing __eq__ and return that, I am afraid.

于 2013-11-13T16:38:51.793 回答