Additional Unpacking Generalizations for assignment
Hello developers. I like star expression. It would be even better if we could extend the idea of PEP448 to provide the following syntax at the time of assignment. ``` python a, b, d, **_ = **{"a":0, " b":1, "c":2}, d=3 ``` Ideally, it would be nice to be able to provide the same syntax when calling the function. I think this applies to the zen of python called "There should be one-- and preferably only one --obvious way to do it.". ``` python a, *args, b, **kwargs = 1, 2, *[3,4], **{"b", 4}, c=4 ( a: str, *args, b: list = [], **kwargs ) = 1, 2, *[3,4], **{"b", 4}, c=4 func = lambda( a: str, *args, b: list = [], **kwargs ): print(a, b) ``` But it would be difficult because it would conflict with various syntaxes(etc. PEP 3132). We may have to declare it explicitly to avoid confusion. For example, prepare a keyword for unpack the positional arguments and keyword arguments at the same time. ``` a, *args, b, **kwargs = ***(1, 2, *[3,4], **{"b", 4}, c=4) ``` You can now explicitly recognize that you are using the behavior of the new assignment. I hope this is an idea to develop python. Thanks.
Hello developers. I like the star expression. It would be even better if we could extend the idea of PEP448 to provide the following syntax at the time of assignment. ``` python a, b, d, **_ = **{"a":0, " b":1, "c":2}, d=3 # Expressed as a function, it looks like this: def func(a, b, d, **_): ... func(**{"a":0, " b":1, "c":2}, d=3) ``` This is useful for splitting the dictionary Ideally, it would be nice to be able to provide the same syntax when calling the function. I think this applies to the zen of python called "There should be one-- and preferably only one --obvious way to do it.". ``` python a, *args, b, **kwargs = 1, 2, *[3,4], **{"b", 4}, c=4 ( a: str, *args, b: list = [], **kwargs ) = 1, 2, *[3,4], **{"b", 4}, c=4 func = lambda( a: str, *args, b: list = [], **kwargs ): print(a, b) ``` But it would be difficult because it would conflict with various syntaxes(etc. PEP 3132). We may have to declare it explicitly to avoid confusion. ``` python # etc a, b, *args, c = (1, 2, 3, 4, 5) ``` For example, prepare a keyword for unpack the positional arguments and keyword arguments at the same time. ``` python a, *args, b, **kwargs = ***(1, 2, *[3,4], **{"b", 4}, c=4) ``` I hope this is an idea to develop python. Thanks.
Dear 笹原康央, This sounds interesting. The assignment could be done e.g., like this. ```python def assign(variables): return f''' for _variable, _value in (lambda {variables}: locals())( *_values, **_assignments ).items(): exec(f'{{_variable}} = _value') ''' def name_update(_namespace, /, *_values, **_assignments): return _namespace.update(locals()) name_update(globals(), 1, 2, *[3,4], **{"b":4}, c=4 ) exec(assign( "a, *args, b, **kwargs" )) def f(): name_update(locals(), 1, 2, *[3,4], **{"b":4}, c=4 ) exec(assign( "a, *args, b, **kwargs" )) return locals() ``` This is certainly much more writing than an assignment statement. (It works also with global and nonlocal variables. One should remove the side effects, which shouldn't be hard with a context manager for instance. The code is not for assignment to the variable "_variable", "_value", or "exec". I haven't thought how to make assignment to any variable possible. Finally, the necessity to call exec requires care.) Best regards, Takuo Matsuoka
Hi Takuo Matsuoka. Thank you for your interest in the idea of applying keyword unpack argument assignments at the time of assignment. Great magic code, you could implement this at the software level. But again it's not useful unless it's syntactically supported. Currently in Python even this syntax is a syntax error(cannot give type hint). ``` ( a: int, b: int ) = (1, 2) ``` If this syntax improves in the future, I would like unpacking keywords to be supported as well. ``` ( a: int, b: int, **kwargs ) = **{"a": 1, "b": 2, "c": 3} ``` 2021年9月2日(木) 23:01 Matsuoka Takuo <motogeomtop@gmail.com>:
Dear 笹原康央,
This sounds interesting. The assignment could be done e.g., like this.
```python def assign(variables): return f''' for _variable, _value in (lambda {variables}: locals())( *_values, **_assignments ).items(): exec(f'{{_variable}} = _value') '''
def name_update(_namespace, /, *_values, **_assignments): return _namespace.update(locals())
name_update(globals(), 1, 2, *[3,4], **{"b":4}, c=4 ) exec(assign( "a, *args, b, **kwargs" ))
def f(): name_update(locals(), 1, 2, *[3,4], **{"b":4}, c=4 ) exec(assign( "a, *args, b, **kwargs" )) return locals()
``` This is certainly much more writing than an assignment statement. (It works also with global and nonlocal variables. One should remove the side effects, which shouldn't be hard with a context manager for instance. The code is not for assignment to the variable "_variable", "_value", or "exec". I haven't thought how to make assignment to any variable possible. Finally, the necessity to call exec requires care.)
Best regards, Takuo Matsuoka
participants (2)
-
Matsuoka Takuo
-
笹原康央