Parameter passing in Python * and * * problem, passing parameters in a tuple and dict way in Python, sometimes you see such a function definition:
def p1 (*arg1,**arg2):
Pass
There is also a function call to see this:
I=5
function (*i)
What does all this mean?
1. Incoming parameters to generate tuple and dict
def p1 (*A1,**A2):
Print A1, '/N ', A2
P1 (1,2,3,4,arg5=5,arg6=6)
The result is:
(1,2,3,4)
{' Arg5 ': 5, ' Arg6 ': 6}
2. Incoming tuple or dict parse into a parameter sequence
def Q1 (ARG1,ARG2,ARG3,ARG4):
Print Arg1,arg2,arg3,arg4
tu= (1,2,3,4)
print ' Extract from tuple/n '
Q1 (*TU)
di={' arg1 ': 1, ' arg2 ': 2, ' Arg3 ': 3, ' Arg4 ': 4}
print '/nextract from dict/n '
Q1 (**DI)
The result is:
Extract from tuple
1234
Extract from Dict
1234
They are reciprocal processes that are useful when you need to transfer functions and function parameters.
Such as:
def worker1 (arg1):
Print Arg1
def worker2 (ARG1,ARG2):
Print Arg1,arg2
Def calling (Callable,arg):
Callable (*arg)
If __name__= "__main__":
Calling (Worker1, (1,))
Calling (Worker2, ())
Problems with parameter passing * and * * in Python