Re: Fallback for operator and other dunder methods
On Wed, 26 Jul 2023 at 16:52, Dom Grigonis wrote: > > Could you give an example? Something isn’t working for me. > This is a metaclass: class Meta(type): ... class Demo(metaclass=Meta): ... In order to catch those kinds of attribute lookups, you'll need the metaclass to hook them. And you might need to use __getattribute__ rather than __getattr__. However, there may also be some checks that simply look for the presence of the attribute (see: slots), so you may find that it's even more complicated. It's usually easiest to just create the slots you want. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Fallback for operator and other dunder methods
Dom Grigonis wrote at 2023-7-26 05:22 +0300: > ... >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. Proxying is a good approach: you might have a look at `dm.reuse.proxy.OverridingProxy` (--> `dm.reuse` on PyPI). -- https://mail.python.org/mailman/listinfo/python-list
Re: Fallback for operator and other dunder methods
Tried exactly that and didn’t work. Neither __getattr__, nor __getattribute__ of meta is being invoked. > On 26 Jul 2023, at 10:01, Chris Angelico via Python-list > wrote: > > On Wed, 26 Jul 2023 at 16:52, Dom Grigonis wrote: >> >> Could you give an example? Something isn’t working for me. >> > > This is a metaclass: > > class Meta(type): >... > class Demo(metaclass=Meta): >... > > In order to catch those kinds of attribute lookups, you'll need the > metaclass to hook them. And you might need to use __getattribute__ > rather than __getattr__. However, there may also be some checks that > simply look for the presence of the attribute (see: slots), so you may > find that it's even more complicated. It's usually easiest to just > create the slots you want. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list