How can we pass optional or keyword parameters from one function to another in Python ?
Answers
Answered by
0
Notice the syntax of the call to func2(). Since kwargs is a Python dict, we can't just pass it to func2(). We have to “unpack” it into a sequence of keyword arguments, and the ** operator does that.
Answered by
0
We can pass optional or keyword parameters from one function to another in Python as follows:
- Consider a function func , which accepts keyword args -
def func (**kwargs):
print('func kwargs:', kwargs)
- Consider another function funcy , which keywords args along with 3 other args
def funcy (a, b, c, **kwargs):
print('other args:', a, b, c)
print('keyword args:', kwargs)
- To pass the keywords from func() into funcy(), this should be done,
def funcy(a, b, c, **kwargs):
print('other args:', a, b, c)
print('keyword args:', kwargs)
func(**kwargs)
Similar questions
Math,
5 months ago
Math,
5 months ago
Computer Science,
11 months ago
Computer Science,
11 months ago
CBSE BOARD X,
1 year ago