I know that mappings by default support the ** operator, to unpack the >mapping into key word arguments.
Has it been considered implementing a dunder method for the **
operator so you could unpack an object into a key word argument, and
the developer could choose which keywords would be generated (or could
even generate 'virtual' attributes).
On 08Feb2024 12:21, tony.flury@btinternet.com <tony.flury@btinternet.com> wrote:
I know that mappings by default support the ** operator, to unpack the >mapping into key word arguments.
Has it been considered implementing a dunder method for the **
operator so you could unpack an object into a key word argument, and
the developer could choose which keywords would be generated (or could
even generate 'virtual' attributes).
Can you show us why you think that would look like in code?
Note that Python already has `a ** b` to raise `a` to the power of `b`,
and it has a bunder method `__pow__` which you can define.
In order for the "splat" operator to work, the type of the object must populate slot `tp_as_mapping` with a struct of this type: https://docs.python.org/3/c-api/typeobj.html#c.PyMappingMethods and
have some non-null implementations of the methods this struct is
supposed to contain.
I can do this in C, but I cannot think of a way to do this in Python
proper.
Looks like it can simply be done in Python, no tp_as_mapping needed.
But, more to the point: extending collections.abc.Mapping may or may
not be possible in OP's case.
Also, if you are doing this through inheritance, this seems really >convoluted: why not just inherit from dict? -- less methods to
implement, less stuff to import etc.
That said, I have some classes which subclass dict, int, str and
namedtuple.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 48:18:46 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,571 |