Fallback for operator and other dunder methods

Chris Angelico rosuav at gmail.com
Wed Jul 26 02:40:20 EDT 2023


On Wed, 26 Jul 2023 at 12:23, Dom Grigonis via Python-list
<python-list at python.org> wrote:
> print(a + 1)            # TypeError: unsupported operand type(s) for +: 'A' and 'int'
>
> Is there a way to achieve it without actually implementing operators?
> I have looked at Proxy objects, but they do not seem suited to achieve this. (e.g. wrapt)

These kinds of special methods are not looked up on the object, but on
the type. It's more like type(a).__add__(a, 1). So you would need a
metaclass for this.

ChrisA


More information about the Python-list mailing list