Le 13/09/2018 à 10:07, Jonathan Fine a écrit :
Now for my opinions. (Yours might be different.)
First, it is my opinion that it is not reasonable to insist that the
argument after ** must be a mapping. All that is required to construct
a dictionary is a sequence of (key, value) pairs. The dict(iterable)
construction proves that point.
Second, relaxing the ** condition is useful. Consider the following.
>>> class NS: pass
>>> ns = NS()
>>> ns.a = 3
>>> ns.b = 5
>>> ns.__dict__
{'b': 5, 'a': 3}
>>> def fn(**kwargs): return kwargs
>>> fn(**ns)
TypeError: fn() argument after ** must be a mapping, not NS
>>> fn(**ns.__dict__)
{'b': 5, 'a': 3}
I don't know about namespaces. It's probably been a concept I've often
used without putting a name on it.
But for dataclasses, I'd find it quite useful to have
{**my_data_class}
be a shortcut to
{**dataclasses.asdict(my_data_class)}
It's most likely what we'd want to achieve by unpacking a dataclass (or
at least, to my opinion). I'm not sure about the internals and the
weight of such a feature, but I guess a toy implementation would just
be, whenever we should raise a TypeError because the variable is not a
mapping, to check whether it's a dataclass instance, and if so, call
asdict on it, and return its result.
I'm not sure I'm not off-topic though...
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/