Fallback for operator and other dunder methods

Dom Grigonis dom.grigonis at gmail.com
Wed Jul 26 02:52:48 EDT 2023


Could you give an example? Something isn’t working for me.

> On 26 Jul 2023, at 09:40, Chris Angelico via Python-list <python-list at python.org> wrote:
> 
> 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
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list