I really, really like the idea of @joshuabambrick making it possible to shorten
my_function(
my_first_variable=my_first_variable,
my_second_variable=my_second_variable,
my_third_variable=my_third_variable,
)
to something else. But I am not so convinced with the syntax
my_function(=my_first_variable, =my_second_variable, =my_third_variable)
or even
my_function(my_first_variable=, my_second_variable=, my_third_variable=)
My input is highly subjective, but I just do not see the syntax visually appealing, and in my opinion, it would not make Python code look nicer. The original problem though is something Iâve stumbled upon a million times and wished that python had a clean solution for.
alternative: autodict
Consider having a set of variables with values:
my_first_variable = 'First value'
my_second_variable = 'Second value'
my_third_variable = 'Third value'
Many of the solutions could be handled just by adding a special dictionary class which could be populated with following syntax (be it autodict
or pundict
or something else):
>>> autodict(my_first_variable, my_second_variable, my_third_variable)
{'my_first_variable': 'First value',
'my_second_variable': 'Second value',
'my_third_variable': 'Third value'}
This would be quite easy to use in function call:
my_function(**autodict(my_first_variable, my_second_variable, my_third_variable))
alternative: &dict
It could, alternatively, be syntactic sugar building on top of dict
with for example &
operator:
my_function(**&dict(my_first_variable, my_second_variable, my_third_variable))
alternative: @dict
We have used to â@â being something used to decorate things. So it could also be
my_function(**@dict(my_first_variable, my_second_variable, my_third_variable))
alternative: & or @ before items
Other solution, similar to what @malemburg mentioned
my_function(**dict(&my_first_variable, &my_second_variable, &my_third_variable))
or
my_function(**{&my_first_variable, &my_second_variable, &my_third_variable})
or then with @
my_function(**dict(@my_first_variable, @my_second_variable, @my_third_variable))
alternative: double curly braces
We have {a, b, c}
for sets, but what if {{a, b, c}}
would mean an autodict?
my_function(**{{my_first_variable, my_second_variable, my_third_variable}})
I really hope that we as python community can find some way to get the functionality to the language!