A detailed description of function parameters in Python

Source: Internet
Author: User
I. Parameter pass-through rule

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

The keyword parameter allows 0 or any parameter to be passed in, which is automatically assembled into a dict when the function is called;

1. Pass in the variable parameter:

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

The above definition functions are used as follows:

Pass in multiple parameters,

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

Pass in a list,

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

2. Incoming keyword parameter:

>>> 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 functions:

>>> info = {' City ': ' Guangzhou ', ' job ': ' Engineer '}>>> >>> person (' luhc ', ' **info ') Name:luh C age:24 Other: {' City ': ' Guangzhou ', ' job ': ' Engineer '}

Note: The function person obtains a copy of the parameter info, and the change within the function does not affect the value of info

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

# by * Split to specify key parameter name >>> def person (name, age, *, City, Job): ...   Print (' name: ', Name, ' Age: ', Age, ' City: ', City, ' job: ', job) ... >>> >>> person (' LUHC ', city= ' Gu Angzhou ', job= ' engineer ') NAME:LUHC age:24 City:guangzhou job:engineer# If the parameter name is not within the defined range in the passed in argument, an exception will be thrown >>> Person (' LUHC ', +, city= ' Guangzhou ', jobs= ' engineer ') Traceback (most recent call last): File "
 
  
   
  ", line 1, in< c4/>
   
    
   Typeerror:person () got an unexpected keyword argument ' jobs ' >>>
  
   
 
  

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

# omitted with * as partition, specify key 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 args: (' A ', ' B ') City:guangz Job:engineer>> ;> # Similarly, if you pass in a parameter name that is not specified by the keyword argument, throw an exception >>> person (' LUHC ', ' a ', ' a ', ' B ', city= ' Guangz ', job= ' engineer ', test= ' a ') Traceback (most recent): File "
 
  
   
  ", line 1, in 
  
   
    
   Typeerror:person () got an unexpected ke Yword argument ' test ' >>>
  
   
 
  

Two, the parameter combination uses:

The order of the parameter definitions must be: required, default, variable, named keyword, and keyword 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)

The above is the whole content of this article to you, hoping to understand the Python function parameters of the transfer of some help

  • Related Article

    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.