Python function parameters (python3.5), pythonpython3.5

Source: Internet
Author: User

Python function parameters (python3.5), pythonpython3.5
Function Definition

A function is an encapsulation of statement blocks that can complete certain functions. A statement block composed of a function name, a list of parameters, and multiple statements is defined as follows:

Def func_name (argrs): Statement Block

Note:


Function call
ret = func_name(args)

Note:

>>> print(1,2,sep='\n')12
>>> a = 1, b= 2

>>> print(a, b, sep='\n')
12

Function return

  • When a return statement function is returned, the subsequent statements are no longer executed.
  • The function can only return a single value. This value can be of any data type.
Function Parameters

The parameter may contain four types of parameters: location parameter, variable location parameter, keyword-only parameter, and variable keyword-only parameter.

Real parameters of function parameters

The matching sequence between real parameters (location parameters and keyword parameters) and form parameters (location parameters, variable location parameters, keyword-only parameters, and variable keyword-only parameters) can be understood as follows:

Deconstruct of parameters in real parameters
  • When calling a function, you can use ** or ** before the set type to unbind the structure of the set element and extract all parameters as the real parameters of the function.
  • For non-dictionary types, use * to describe the Location Parameter
  • The dictionary type uses ** to describe the keyword parameter.
  • The number and type of extracted elements must match the requirement of the parameter.
Default parameters

The default parameters are evaluated and saved when the def statement is run, rather than when the function is called. Internally, python combines all default parameters to generate a tuples and save them as an attribute of the function (_ defaults. Because attributes are appended to the function itself, all calls to the function use the same object, so you must be careful when modifying variable default parameters.

>>> Def saver (x = []):... x. append (1 )... print (x)... >>> saver. _ defaults _ # How to view object attributes, apply the variable name of the object and add the attribute name ([],) >>> saver () [1] >>> saver. _ defaults _ ([1],) >>> saver () [1, 1]

 

If this is not the expected behavior, move the expression of the default parameter value to the function body:

>>> Def saver (x = None ):... x = x or [] # If no parameter is input, x is None by default. The or operation returns an empty list... x. append (1 )... print (x)... >>> saver () [1] >>> saver () [1]

Or, returns the first true value from left to right. If all values are False, the last value is returned. If the input x parameter is 0, an empty list, or an empty set, it will be replaced with an empty list [], which may not meet expectations. We can use the if statement:

>>> def saver(x=None):...     if x == None:...         x = []...     x.append(1)...     print(x)

 

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.