Functions in Python

Source: Internet
Author: User

Functions in Python

1. Overview:

A function is a reusable program segment. You give a statement block a name, and then you can use this name to run the statement block multiple times anywhere in your program. This is called a function call.

The function is defined by the keyword def. The def keyword is followed by the identifier name of a function, followed by a pair of parentheses. Variable names can be included in parentheses, which end with a colon.

Next is a statement, which is the function body.

2. function parameters:

(1). participate in real parameters and positional parameters.

(2). default parameters.

(3). Keyword parameter.

(4). Varargs parameters:

Sometimes, you may want to define a function that can obtain any number of parameters, which can be achieved by using the * number.

When we define a parameter with a star, such as * param, all common parameters after that are collected as a tuple called 'param.

Similarly, when we define a parameter with two stars, such as ** param, all the keyword parameters starting from that point will be collected as a dictionary called 'param.

Varargs parameters are also called collection parameters. There are many tips for using small rules. For example, the setting of function parameters in Python is sequential. Otherwise, an error may occur. Specific problems

Simple. Even if it happens in practice, it is easy to make the same and correct.

(5). Keyword-only parameter:

If you want to specify the key parameter keyword-only instead of the location parameter, you can declare it after the parameter with a star. (Keyword parameters or common parameters cannot be stated after two stars are included in the test ).

If you want to use the keyword-only parameter without the star parameter, you can simply use an empty star without a name, such as def total (initial = 5 ,*, vegetables ).

3. Scope, lifetime, or visibility of variables:

Variables in Python are divided into global variables, local variables, and non-local variables according to the scope. Simply literally, you can intuitively understand the relationship between the scope of a variable and the defined position.

If you really want to modify the global or non-local variables in the function body, you can use the global statement and nonlocal Statement (nonlocal statement is a new feature of Python3,

In the old Python, you can only consider using a list instead of a variable. It seems that it is related to the storage and shielding mechanism of the variable, and further study is required ).

4. Docstrings of the function:

Because the document string helps your program documentation to be easier to understand, you should try to use it. You can even restore the document string from the function when the program is running!

The string in the first logical line of the function is the document string of the function. Note that DocStrings also applies to modules and.

A document string is a multi-line string whose first line starts with an uppercase letter and ends with a full stop. The second line is empty, and the detailed description starts from the third line. It is best to use your own functions

Follow this Convention when using document strings.

You can use _ doc _ (note Double underline) to call the document string attribute of the printMax function (belongs to the function name ). Remember that Python regards everything as an object, including this function.

You can also use help (printMax ).

5. return Statement of the Function

6. Embedded functions and closures:

Embedded functions, as the name implies, are the functions defined in the function body, also known as internal functions.

A closure is an embedded function. In short, a closure gets different results based on different configuration information. Closures often have the following features: Embedded functions call external variables of their own functions within external functions,

It is a non-local variable. An external function that contains the closure usually returns the function name of the closure.

For a deep understanding of closures, please go to this hero's blog http://www.cnblogs.com/ma6174/archive/2013/04/15/3022548.html.

7. lambda expressions:

Lambda expressions are anonymous functions. It can only consist of one row. For example, g = lambda x: x * x. There can be one or more parameters before the colon, followed by the return value of the function, expressed in an expression,

There is only one expression. Lambda expressions can simplify the code and enhance readability. writing some short functions that need to be called multiple times as anonymous functions can greatly improve the program running efficiency.

Lambda expressions in Python3 are often used for sorting.

In many languages, lambda expressions are similar to the macro definition in C language or inline in C ++.

8. Several awesome BIF:

Filter (), map (), reduce (),

If none of the three functions can be used, do not say you have learned Python.

9. Recursive Design in functions. As you know, recursion is always the most fascinating and optimal place in programming. There is a saying: "ordinary programmers use iterations, and geniuses use recursion ".

 

 

 

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.