The return value of the function:
can return any type
No return words returned: none
Return returns the run of the terminating function
Use of functions:
You must first define and then call the
function definition: Similar to the definition of a variable, if it is called without prior definition, it is equivalent to referencing a non-existent variable name
##定义阶段#def foo ():#print (' from foo ')#Bar ()##def Bar ():#print (' from bar ')###调用阶段#foo ()#definition phase: Only the syntax is detected and no code is executed#def func ():#if 1>2#print (' Hahahahahahah ')defFunc ():#syntax is fine, logic is a problem, referencing a non-existent variable name
View Code
Function parameters:
Real-participation parameters (actual arguments passed in are called arguments)
Positional parameters: Parameters defined sequentially in order from left to right
def foo (x, y ): Print (x) Print (y) # Parameters defined by location, must be passed value, one more not, one less can not, according to the location of the incoming Foo (for each )
View Code
Keyword parameter: When the argument is defined, it is defined as the Key=value form
def foo (x, y ): Print (x) Print (y) foo(y =10,x=1) foo (y# keyword parameter can not correspond to parameter one by one like positional arguments, need to name the value
View Code
Attention:
Problem one: The positional argument must precede the keyword argument
Problem two: The form of an argument can be either a positional argument or a key argument, but a parameter cannot repeat the value
Default parameters: In defining the function phase, the parameter is already assigned a value, the definition stage has the value, the call stage can not pass the value
def func (x,y=10): The default value is given in the# parameter , which can be used without passing the value in the argument, then the print(x) # is run by default when the value is passed, the print (Y) func (1,20) func (1) is run as the value passed in.
View Code
#形参的应用: Values that change frequently need to be defined as positional parameters,
# values are the same in most cases and need to be defined as default parameters
Default parameters Note the issue:
First, the default parameters need to pay attention to the problem one: must be placed behind the position parameter
Second, the default parameters need to pay attention to the issue of the second: default parameters are usually defined as immutable types
Three, the default parameters need to pay attention to the problem three: the default parameter is only assigned once in the definition phase
Variable length parameter: variable length refers to the number of arguments is not fixed
#按位置定义的可变长度的实参: *
#按关键字定义的可变长度的实参: * *
defFunc (X,y,*args):#x=1,y=2,args= (3,4,5,6) #* receives parameters other than x, Y, and assigns the value to args in the form of tuples Print(x, y)Print(args) func (1,2,3,4,5,6) defFunc (X,y,*args):#args= (3,4,5,6) Print(x, y)Print(args) func (1,2,* (3,4,5,6))#* The number is foo (1,2,3,4,5,6) after it is opened deffunc (x, Y, z):Print(x, Y, Z) func (1,* (2,3))#func (#同上)Func (* (2,3))#func (2,3) defFunc (X,y,**kwargs):#x=1,y=2,kwargs={' A ': 1, ' B ': 3, ' Z ': 3}#** is used to receive keyword parameters and is assigned to Kwargs in dictionary form Print(x, y)Print(Kwargs) func (1,Y=2,Z=3,A=1,B=3)#keyword parameter must be placed after position parameter defFunc (X,y,**kwargs):#x=1,y=2,**kwargs=**{' A ': 1, ' B ': 3, ' Z ': 3} Print(x, y)Print(Kwargs) func (1,y=2,**{'a': 1,'b': 3,'Z': 3})#* * Disassemble as func (1,y=2,z=3,b=3,a=1) defFunc (x,y=1,z=1): Print(x, Y, Z) func (**{'y': 2,'x': 1,'Z': 3})# defWrapper (*args,**kwargs):#can accept any type of argument of any length Print(args)Print(Kwargs) wrapper (1,2,3,3,3,3,3,X=1,Y=2,Z=3)
View Code
Named keyword parameter: The parameters defined in the * after, such parameters, must be passed the value, and the truth must be in the form of a keyword to pass the value
Nested use of functions:
Defined:
Def f1 ():
def f2 ():
def f3 ():
print (' from F3 ')
print (' from F2 ')
f3 ()
print (' From F1 ')
F2 ()
# Print (F1)
F1 ()
def Max2 (x, y): if x > y: return x else : Span style= "COLOR: #0000ff" >return y def max4 (a,b,c,d): Res1 =max2 (A, b) # Span style= "COLOR: #008000" >23 res2=max2 (res1,c) # 23 res3=max2 (res2,d) # 31 Span style= "COLOR: #0000ff" >return Res3 Print (max4 (11,23,-7,31))
View Code
A function is the first class of objects: Refers to functions that can be passed as data
deffunc ():Print('From func')#can be referencedf=func#can be used as a parameter to a functiondeffunc ():Print('From func')deffoo (x):Print(x) x () foo (func)#can be used as the return value of a functiondeffoo ():Print('From foo')defBar ():returnfoof=Bar ()Print(f)Print(foo) x=0defF1 (): x=1defF2 ():#x=2 Print(x)returnF2F=F1 ()#print (f)f ()#elements that can be treated as container typesdefSelect ():Print('Select function') Func_dic={ 'Select': SELECT,}#Print (func_dic[' select ')#func_dic[' select '] ()##def Select ():#print (' select Func ')##def Delete ():#print (' delete func ')##def Change ():#print (' Change func ')##def Add ():#print (' Add func ')###While 1:#cmd=input (' >>: '). Strip ()#If not cmd:continue#if cmd = = ' SELECT ':#Select ()#elif cmd = = ' Delete ':#Delete ()#elif cmd = = ' Change ':#Change ()#elif cmd = = ' Add ':#Add ()#Else:#print (' Invalid command ')
View Code
Namespaces: A binding relationship that holds names and values
Namespaces are divided into three types
Built-in namespaces: The Python interpreter comes with its own name, and the Python interpreter starts to generate
Global namespaces: File-level-defined names are stored with the global namespace, which is generated when a Python file executes
Local namespaces: Define names inside functions, local namespaces only take effect when functions are called, and end of function calls are invalidated
Load order of the three: built-in namespaces--global namespace--local namespace
Value: Built-in namespaces, global namespace, local namespace
Scope: Scope of action
Global scope: The name of the built-in namespace and the global namespace belong to the global range,
Can be referenced anywhere in the entire file, valid globally
Local scope: The name of the local namespace belongs to the local range,
Only within the function can be referenced, locally valid
Python Programming (quad) functions