I had this idea while reading the topics about PEP 736.
(Thus related : Syntactic sugar to encourage use of named arguments, PEP 736: Shorthand syntax for keyword arguments at invocation, Shorthand notation of dict literal and function call)
I realized these propositions are simply summarized as “self-named arguments packing”.
The creation of a dictionary with self named arguments could be done simply with a triple-star packing
x, y, z = 1, 2, 3
snargs = {***(x, y, z)} # self-named arguments packing
# equivalent to >>> snargs = dict(x=x, y=y, z=z)
snargs # {'x': 1, 'y': 2, 'z':3}
Then unpacking could be also defined in a convenient way, implicitly checking names consistency :
a, b, c = ***snargs # KeyError: <dict> has no key 'a'
x, y, z = ***snargs # self-named arguments unpacking
x, y, z # (1, 2, 3)
This yields a convenient way of using functions with snargs:
def func(***snargs):
x, y, z = ***snargs # self-named arguments unpacking
x, y, z = x + 1, x + z, 3*y + x
return {***(x, y, z)}
func(***(x, y, z)) # {'x': 2, 'y':4, 'z': 7}
But actually, for further convenience, we might have an even more concise way of using it :
def func(***(x, y, z)):
x, y, z = x + 1, x + z, 3*y + x
return {***(x, y, z)}
new_x, new_y, new_z = ***func(***(x, y, z)) # KeyError : no key 'new_x'
x, y, z = ***func(***(x, y, z))
# Any unordered args will be reordered, any modified variable name will raise KeyError
Thus these two signatures would be equivalent
def func_with_default_y(x=x, y=2, z=z) : ...
def func_with_default_y(y=2, ***(x, z)) : ...
Allowing for clarification of functions with long-self-named arguments, for example :
def complicated_function(argument0,
argument_with_default_value = default_value,
**kwargs,
***(my_long_argument_name,
my_other_long_argument_name,
another_one_again,
and_a_last_one_for_fun)):
...
CONS
- Difficulty to implement in python ???
- Complicated new syntax
- Multiple ways for doing the same thing :
x, y, z = ***func(***(x, y, z))
is equivalent to simplyx, y, z = func(x=x, y=y, z=z)
, except for the pros given below.
PROS
- Automatic check for modified names :
Whenever someone would reimplement/refactor a code, if the way a variable is managed is modified, the user could just rename the variables and update some necessary functions, any function not modified subsequently, if using the snargs convention, would raise an error, thus informing the user the modification has not been applied within this function. - Reducing name redundancies (as was the initial purpose of PEP 736), also applicable for simple dict creation. Only one name has to be updated during refactoring, thus fool-proofing functions and dictionary instantiation update.
At the end, this proposition is not really improving “final codes”, but maintainability and refactorability of “temporary codes” might be easier by the fool-proofing properties of the snargs.
Possibly, nobody will want these triple-stars spawning everywhere in python code, as underlined by CON#3.
Thus I am not really convinced this idea will be accepted but I would like to open this discussion anyway… Opinions?