Python functions in the argument _python

Source: Internet
Author: User

First, parameter incoming rule

Variable parameters allow 0 or more parameters to be passed in and automatically assembled into a tuple when the function is called;

Keyword parameters allow 0 or more parameters to be passed in and automatically assembled into a dict when the function is called;

1. Incoming variable parameters:

 Def calc (*numbers):
   sum = 0 for
   n in numbers:
     sum = SUM + N * n * return
   sum

The above definition functions, using the following:

Pass in multiple parameters,

Calc (1, 2, 3, 4)
#函数返回值

Pass in a list,

Nums = [1, 2, 3]
calc (*nums) # Pass the element in the list as a variable parameter to the function
14 # function return value

2. Incoming keyword parameters:

>>> def person (name, age, **kw):
...   Print (' name: ', Name, ' Age: ', age, ' other: ', kw)
... 
>>> 
>>> person (' LUHC ', city= ' Guangzhou ')
NAME:LUHC age:24 Other: {' City ': ' Guangzhou ' }

Similarly, you can pass a predefined dict as a parameter to the above function:

>>> info = {' City ': ' Guangzhou ', ' Job ': ' Engineer '}
>>> 
>>> person (' LUHC ', 24, * * info)
NAME:LUHC age:24 Other: {' City ': ' Guangzhou ', ' Job ': ' Engineer '}

Note: A copy of the parameter info is obtained by the function person, and changes in the function do not affect the value of info

3. In the keyword parameter, you can restrict the name of the keyword parameter:

# by * Split to specify keyword parameter name
>>> def person (name, age, *, City, Job):
...   Print (' name: ', Name, ' Age: ', Age, ' City: ', City, ' job: ', job ')
... 
>>> 
>>> person (' LUHC ', city= ' Guangzhou ', job= ' engineer ')
NAME:LUHC age:24 City: Guangzhou job:engineer

# If the parameter name is not in the defined range, the exception >>> person will be thrown
(' LUHC ', #, city= ' Guangzhou ', Jobs= ' engineer ')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
Typeerror:person () got an unexpected keyword argument ' jobs '
>>>

In addition, if a variable parameter has been specified in the function, the * can be omitted as follows:

# omitted to be split with *, specify keyword parameter name
>>> def person (name, age, *args, City, Job):
...   Print (' name: ', Name, ' Age: ', age, ' args: ', args, ' City: ', City, ' job: ', job)  
... 
>>> 
>>> person (' luhc ', ', ' A ', ' B ', city= ' Guangz ', job= ' engineer ') NAME:LUHC age:24
: (' A ', ' B ') City:guangz job:engineer
>>> 
# Similarly, if you pass in a parameter name that is not specified by the keyword parameter, the exception >>> person is thrown
(' LUHC ' ,, ' A ', ' B ', city= ' Guangz ', job= ' engineer ', test= ' a ')
Traceback (most recent call last):
 File <stdin> ", line 1, in <module>
Typeerror:person () got a unexpected keyword argument ' test '
>>>

Second, the combination of the use of parameters:

The order of the parameter definitions must be: required, default, variable, named keyword, and key parameters

Def f1 (A, B, c=0, *args, **kw):
  print (' A = ', A, ' B = ', B, ' C = ', C, ' args = ', args, ' kw = ', kw)

def F2 (A, B, c=0 , *, D, **kw):
  print (' A = ', A, ' B = ', B, ' C = ', C, ' d = ', D, ' kw = ', kw)

  Above is what this article is all about, and hopefully it will help you understand the passing of Python's function parameters

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.