Assume I have the following function:
def func(a, b=0, c=1, d=2,...):
# Code goes here
I want to use it inside another function like this:
def do_sth(...):
func(...)
do_sth(...)
The number of parameters of func is big. What should I do to pass (optional) params to func when calling do_sth, because the following method is usable but not pythonic:
def do_sth(a, b, c...):
func(a=a, b=b,c=c...)
do_sth(a, b, c...)
Pass a dictionary like this (using ** before params dict):
params = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
}
func(**params)
To pass list, use *:
params = [1,2,3,4]
func(*params)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With