Python study note 5, functions and their parameters, and python learning note Functions

Source: Internet
Author: User

Python study note 5, functions and their parameters, and python learning note Functions

How to customize a function in Python: The format is def function name (function parameter ):

Content

After writing a function in interactive mode, press ENTER twice to return >>>

Pass is used as a placeholder for a function that is null (which means nothing is done). That is to say, if you don't want to write any function, use the pass placeholder first, and then let the program run;

Built-in functions can be used for data type check.isinstance(object,calssinfo)Implementation: If the parameter object is an instance of classinfo, or the object is an instance of a subclass of the classinfo class, True is returned. If an object is not of the specified type, the returned result is always False.

Function parameters:

Default function parameter: by default, the value of n is 2. Therefore, only the first parameter can be uploaded when the parameter is passed. If the second parameter is input during the call, the default parameters are overwritten. The advantage of this is that it is very convenient to enter the same item every time. If multiple parameters are input sequentially by default, if you want to change the default parameter value during this call, you can directly write the parameter name = parameter value when calling the function. Note that,

Default parameters must point to unchanged objects

Def power (x, n = 2 ):
S = 1
While n> 0:
N = n-1
S = s * x
Return s

 

Variable Parameter: When a function with uncertain parameters is encountered, you must use list or tuple as the parameter. However, the input parameters must be list or tuple. If you use variable function parameters, You can omit this. You only need to add a * sign before defining the parameters. * List is to pass in all the elements in the list as variable parameters.

 

Keyword parameter: the parameter is represented by ** keyword. It is passed in the form of "what" = "what" and is automatically assembled into a dict. The keyword parameter is not a required parameter; however, you can also input any number of keyword parameters.

Keyword parameters are used to expand parameters. For example, some parameters are required during user registration and some parameters are optional. When users provide more parameters, they can also receive them, and directly organized into a dict;

 

Name keyword parameter: The name keyword parameter is used to restrict the parameter names that can be passed in by the caller (an error is returned if the caller is forced to pass in the name keyword parameter without passing in), and the default value is also available. That is to say, this function only accepts the named keyword parameter. This is to limit the number of keyword parameters.

You can also set the default value. In this case, you do not need to input the default name keyword parameter.

 

Combination parameters:

In Python, mandatory parameters, default parameters, variable parameters, keyword parameters, and naming keyword parameters can be used in any combination, which is very convenient. variable parameters cannot be mixed with naming keyword parameters.

The order of parameter definitions must be: mandatory parameter, default parameter, variable parameter, name keyword parameter, and keyword Parameter

 

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.