On 26 Jul 2023, at 09:40, Chris Angelico via Python-list <[email protected]> wrote:
On Wed, 26 Jul 2023 at 12:23, Dom Grigonis via Python-list <[email protected]> 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
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)
Could you give an example? Something isn’t working for me.
...
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.
On 26 Jul 2023, at 10:01, Chris Angelico via Python-list <[email protected]> wrote:
On Wed, 26 Jul 2023 at 16:52, Dom Grigonis <[email protected]> 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
base_class = type( name, (object,), dic )
Is there a way to achieve it without actually implementing operators?
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 <[email protected]> wrote:
On Wed, 26 Jul 2023 at 16:52, Dom Grigonis <[email protected]> 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
On 2 Aug 2023, at 19:54, Edmondo Giovannozzi via Python-list <[email protected]> wrote:
Il giorno mercoledì 26 luglio 2023 alle 20:35:53 UTC+2 Dom Grigonis ha scritto:
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 <[email protected] <http://python.org/>> wrote:
On Wed, 26 Jul 2023 at 16:52, Dom Grigonis <dom.gr <http://dom.gr/>[email protected] <http://gmail.com/>> 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
For numpy arrays you can find some suggestion at: https://numpy.org/doc/stable/user/basics.dispatch.html <https://numpy.org/doc/stable/user/basics.dispatch.html>
--
https://mail.python.org/mailman/listinfo/python-list <https://mail.python.org/mailman/listinfo/python-list>
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 136:26:04 |
| Calls: | 12,087 |
| Files: | 14,997 |
| Messages: | 6,517,376 |