2020-12-28 Christopher Barker <[email protected]> dixit:
> I don't know about the OP, but all I wanted was a clear definition of
> the part of the API needed to support **, and apparently it's a
> keys() method that returns an iterator of the keys, and a __getitem__
[...]
To be more precise: an *iterable* of the keys -- not necessarily an
*iterator*; it can be, for example, a list or string:
>>> def fun(**kwargs):print(kwargs)
...
>>> class C:
... def keys(self): return list('abc')
... def __getitem__(self, key): return 42
...
>>> c = C()
>>> fun(**c)
{'a': 42, 'c': 42, 'b': 42}
And, even, the `keys()` method does not need to be defined on the
class level -- it can be a callable attribute of an *instance*:
>>> class D:
... def __getitem__(self, key): return 42
...
>>> d = D()
>>> d.keys = lambda: 'abc'
>>> fun(**d)
{'a': 42, 'c': 42, 'b': 42}
Cheers,
*j
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/F3QHN73PMFEM3W67YYS4O3YYSJ6B7VX3/
Code of Conduct: http://python.org/psf/codeofconduct/