Python-Understanding functions, python-Understanding Functions

Source: Internet
Author: User

Python-Understanding functions, python-Understanding Functions
Function Definition

Def functionname (arg ):

Suite

Return [expression]

1. keywords used when def defines a function

2. functionname function name

3. The parameter name of the arg function. Different parameters are input, and the return value is also different. The function is implemented by passing parameters.

4. Suite is the code segment that implements function functions.

5. The expression followed by return is the return of the function.

Function call

Functionname (arg) function name (parameter)

The return expression value is returned after the function is called.

Parameters used for function calling must be passed in strict accordance with the parameters defined in the function.

Return is followed by an expression and cannot be followed by a value assignment statement.

Required parameters of a function: (when defining a function)

When defining a function parameter, only the parameter name has no value. This method makes it necessary to pass in a parameter to call the function normally. The number and sequence of required parameters must be the same as that of the definition, unless you use a keyword parameter to change the Parameter order.

Default parameter: (when the function is defined)

The initial value is provided when the function parameter is defined. If the parameter is not input to this parameter during the call, the function uses the default value during the definition, if a value is passed in to this parameter, the default value will be discarded and the value you passed in will be used.

Keyword parameter: (for function calling)

A keyword parameter is used to input the parameter names and corresponding values of the defined function into the function during parameter passing during function calling. In this case, the order of input parameters does not need to be considered.

Variable Length Parameter: (both definitions and calls can be used)

Use this function when defining the number of parameters to be passed in.

* Args indicates the tuples. args indicates the name of the tuples. The input value is the element of the tuples. This parameter does not accept keyword parameters.

** Kwargs indicates the dictionary. kwargs is the name of the dictionary. When you call this function, use a keyword parameter to take the parameter as the dictionary element. The parameter name is the dictionary key and the parameter value is the dictionary value.

These two parameters can be used at the same time, but must be preceded by * args, followed by kwargs. When a function is called, the directly passed value must be placed before the keyword parameter.

* Args and ** kwargs can also be used in function calls.

When we understand the number and sequence of parameters for function definition, we can combine the parameters to be passed in into a metagroup and use the * + tuples for passing parameters to call the function.

We use the parameter name of the parameter to be passed as the dictionary key, and the value of the parameter as the dictionary value, then, the dictionary consisting of the parameters to be passed in is used with the ** + dictionary name for passing parameters to call the function.

 

Note: When calling a function, when passing values based on the number and type of parameters during Function Definition, you must be clear about the type of parameters during function definition, the Return Value of the function can be called correctly only when the passed parameter matches the definition.

 

Definitions of global variables and local variables:

Global variables:Defines variables with a global scope outside the function and can be accessed within the entire program scope.

Global variables can be accessed in the function but cannot be modified in the function.

Local variables:Variables defined within a function with a local scope can only be accessed within the declared function.

 

Variable scope:

Where the variable is assigned a value determines the region where the variable is applied.

 

Global keyword:

Use the global keyword in the function to declare that this variable is a global variable. When using global to modify this variable in this function, this changes the value of the global variable every time you call this function.

Basic Form of anonymous functions:

Lambda arg: expression

Use the keyword lambda, arg is the function parameter, and expression is the return value of the anonymous function.

It neither contains the control structure nor the return statement. The returned value is only the value obtained after the expression is computed.

Using lambda can save the process of Function Definition and simplify the code.

For some functions that only need to be used once or twice, using lambda does not require function naming.

Built-in function filter, built-in map functions, and closure concepts

A function is nested in the function, and the return value of the external function is the function in return. Then, the function in the function can access the variable of the function outside, we cannot directly call the internal function outside the function, but can call the internal function through the return value of the external function. We call this phenomenon a closure.

Decorator

Using function nesting, the parameter passed in by the outer function is a function object. The internal function processes the passed function and then returns the processed function.

 

Recursive Function callback

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.