"Python" * and * * parameter issues

Source: Internet
Author: User

Problem:
There are two special cases in the function definition of Python, that is, the form of *,** appears.
such as: Def myfun1 (username, *keys) or Def myfun2 (username, **keys) and so on.

Explain:
* Used to pass any of the parameterless parameters, which are accessed in the form of a tuple.

* * Used to process the passing of any parameter with a name, these parameters are accessed using Dict. *

Application:
#########################

# Application of "*"

#########################

>>> def fun1 (*keys):
.. print "Keys type=%s"% type (keys)
... print "keys=%s"% str (keys)
... for I in range (0, Len (keys)):
... print "keys[" + str (i) + "]=%s"% str (keys[i])
...
>>> fun1 (2,3,4,5)
Output the following results:

Keys type=
Keys= (2, 3, 4, 5)
keys[0]=2
Keys[1]=3
Keys[2]=4
Keys[3]=5

#########################

# Application of "* *"

#########################

>>> def fun2 (**keys):
.. print "Keys type=%s"% type (keys)
... print "keys=%s"% str (keys)
... print "name=%s"% str (keys[' name '])
...
>>>
>>> fun2 (name= "VP", age=19)

Output the following results:
Keys type=
keys={' age ': +, ' name ': ' VP '}
Name=vp

"Python" * and * * parameter issues

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.