Problems with parameter passing * and * * in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.