python--function

Source: Internet
Author: User

Function

I. Definition and invocation of functions

function definition: Functions and tools, we use the process of preparing tools as defining a function, and using a prepared tool called a function call.

Conclusion: The function must be first defined and then called.

1. Define a function

def func ():    print ('Hello word') func ()

Note def is the keyword, func is the function name, the parentheses followed by the colon must be written, and the Def and Func are separated by a space

Func (): Calling a value in the body of a function------call called a function

A function name, consisting of numbers, letters, and underscores. Cannot start with a number. cannot be a python keyword

--------Write function should have comment description

Write a function, keyword + function name--------sub-code-----First write the comment-------then write the function body

2. Classification of functions

Built-in functions: No need to define, the Python interpreter is ready.

Such as

Print (Len ('hfgsgj00'))   #求长度print (max ([1,2,3 ,4]))     #最大值print (min ([si,778,si,7865]))  #最小值print (SUM ([4864,5]))   #求和

Custom functions:

Def mylen ():    Pass

3. Return value

Return value: The number returned after return is called the return value

Return and function have three forms of returns

1, there is no return value------is not to write return, the function defaults to return none, only write return no return value function also default returns none

2, return only one value--------followed by a return value, can be followed by any type of value

3, returns multiple values-------return can be followed by any number of any type of returned value, the value returned is in the tuple type

Note: If there is more than one return in the function, only the first return is executed when the function executes and the first return function finishes executing

Second, the parameters

1, Parameter: formal parameter, value accepted at function definition

parameter types and definitions;

1. Default parameter: Set default value

2, Position parameters: According to the position sequence one by one corresponding to the parameter

3, Dynamic Parameters: *arges Accept one or more parameters, group members, **kwargs accept multiple keyword parameters, and make up a dictionary type

2, argument: The value of the real existence, the value passed in when the function is called

1, according to the location of the parameter: according to position one by one corresponding to the incoming parameters

def func (x, y):    print (×)    print (Y) func (ten)

2, according to the key word of the parameter: is the form of variables and the assignment of the form of the parameter

def func (x, y):      print (×)        print (y)    func (y=ten, x=

Three, space naming and scoping
1, three space naming (namespace loading from hard disk to memory)

1, local namespace naming: When the program is running, the call is loaded

2. Global namespace naming: loading from top to bottom in a program run

3. Built-in space naming: loading before running the program

2, the value of the space name (from the inside out to take the value)

1, local call: Local space-------Global space---------built-in space

2, Global Call: Global space-------built-in space

3, Scope: namespaces and scopes are inseparable, because the scope of the row to get the namespace

1, global scope: Takes effect globally, contains built-in namespaces and global namespaces, and can be applied anywhere in the file

2, local scope: Local space name, only within the local scope to take effect

Four, the function name essence

The function name essence is the memory address of the function

1, can be applied

2, can be used as a function parameter and return value

3, can be used as an element of the container type

Five, closures

Closure function: An intrinsic function contains a reference to an outer scope rather than a global role domain name, which is called a closure function-------function defined internally as an intrinsic function

Six, decorator

1, the forming process of the adorner

Grammatical sugar, adorner with parameters, hold residence with function, adorner with return value

Definition: An adorner is a closure function

Function: Extend the function of functions without modifying the original function and its calling method

2, the open closure principle of the adorner

Open for extensions, closed for modifications

3, the main function of the adorner and the fixed structure of the adorner

The main function of adorners: adding functions before and after functions without changing the calling method

The fixed structure of the adorner

def Time (func):                             def inner (*args,**Kwargs):             #函数执行之前要做的                           ret=func (*args,**Kwargs)           #函数执行之后要做的                            return ret                       return Inner                                                  

4, adorner with parameters

def outer (flag): Def timer (func): Def Inn ER (*args,**Kwargs):ifFlag:print (" "what to do before executing a function" ") Re= Func (*args,**Kwargs)ifFlag:print (" "what to do after the function is executed" ")                         returnRereturnInnerreturnTimer @outer (False) def func (): Print (111) func () 


5, a plurality of adorners decorate a function

def wrapper1 (func): Def inner (): Print ('Wrapper1, before Func') func () print ('Wrapper1, after Func')           returnInner def Wrapper2 (func): def inner (): Print ('Wrapper2, before Func') func () print ('Wrapper2, after Func')           returnInner @wrapper2 @wrapper1 def f (): Print ('In F') F () /c10>

python--function

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.