Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: pass multiple parameters to a function from a function in a pythonic way [duplicate]

Tags:

python

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...)
like image 566
Long Luu Avatar asked May 14 '26 15:05

Long Luu


1 Answers

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)
like image 184
programmer365 Avatar answered May 17 '26 03:05

programmer365



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!