Python Quick Learning-functions

Source: Internet
Author: User

Summary of function Definitions:

1. When defining functions, it is necessary to determine the number of function names and parameters;
2. If necessary, first check the data type of the parameter;
3. Inside the function body can return the function result at any time;
4. Function execution complete without return statement, automatically return to none;
5. Functions can return multiple values at the same time, i.e. a tuple;

Def myfunc ():    Pass

  

function parameters

Positional parameters: When a function is called, the two values passed in are assigned sequentially in the order of position
Default parameter: parameter specifies default value;
Note: 1 must be the parameter before the default parameter in the
2 How to set the default parameters-multi-parameter case, the maximum change in front, the change is minimal after the change is the smallest can be the default parameters
3 default parameters must only want to be immutable objects
Variable parameter: First assembled into list or tuple, then passed in.
Keyword parameters: variable parameters allow you to pass in 0 or any of the parameters that are automatically assembled as a tuple when the function is called. The keyword parameter allows you to pass in 0 or any parameter with a parameter name, which is automatically assembled inside the function as a dict
Named keyword parameters:
If you want to restrict the name of the keyword argument, you can use the named keyword argument
When using named keyword parameters, it is important to note that if there are no mutable parameters, you must add a * as a special delimiter. If the missing *,python interpreter will not recognize the positional parameter and the named keyword argument

Parameter combinations
1 the order in which the parameters are defined must be: required, default, variable, named keyword, and keyword parameters
2 for any function, it can be invoked in the form of a func (*args, **kw), regardless of how its arguments are defined

The parameters of the 3python have a very flexible parameter pattern, which can be used to make simple calls and to pass through very complex parameters.
4 default parameters must be used immutable objects, if it is a mutable object, the program will run a logic error.
5 note the syntax for defining mutable and keyword parameters:
*args is a variable parameter, and args receives a tuple;
**KW is the keyword parameter, and kw receives a dict.

Syntax for passing in mutable parameters and key parameters when calling a function:
Variable parameters can be passed directly: Func (1, 2, 3), or the list or tuple can be assembled first, and then passed through *args: Func (* (1, 2, 3));

Keyword parameters can be directly passed in: Func (A=1, b=2), can be assembled dict, and then passed through **kw: func (**{' a ': 1, ' B ': 2}).

Using *args and **kw is a Python idiom, but it can also be used with other parameter names, but it's best to use idioms.

The named keyword parameter is used to limit the name of the parameter that the caller can pass in, as well as providing a default value.

Define a named keyword parameter do not forget to write the delimiter * If there is no mutable parameter, otherwise the definition will be the positional parameter.

Always keep in mind that the less code you have, the more efficient your development

Python Quick Learning-functions

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.