3 ms·
I suppose both observations are reminiscent of the fact that Python has language-level support for named parameters. Given that named and sequential parameters
by kroltan 8y ago
I suppose both observations are reminiscent of the fact that Python has language-level support for named parameters. Given that named and sequential parameters are treated differently everywhere in Python (sequential params must be provided, keyword args are optional, amongst other things), it would be extremely surprising behaviour if the run-time type of the value being spread determined what kind of spread happened.
In JavaScript, the surrounding syntatical context solely decides if it's an object or array splat. But since Python has 2 kinds of parameters, to preserve obviousness when reading code using splats, they went with 2 operators for each parameter kind, and then separated list and dictionary splat analogously.
- int_19h 8y ago> it would be extremely surprising behaviour if the run-time type of the value being spread determined what kind of spread happened. The other reason to distinguish * and double-* is because * works with any iterable, and dicts are themselves iterables (of their keys). So you can actually use * on dicts, it just does something different: >>> d = {'a': 1, 'b' : 2} >>> [*d] ['a', 'b'] You could arguably say that only * makes sense here since it's a list context. But then, as you say, function calls would still be ambiguous because they support both positional and named arguments. This keeps it all unambiguously consistent in all contexts.