python training chapter 3, functions, decorators, modules, one of the built-in functions

content:

Function example
decorator
module
built-in function
1. Function example:

 1. Define the function:
    def fun(args):
      'descriptive information'
      function body
    return return value

  Three forms of defining functions:
    no-argument function
def foo():
print('in the foo')

                        foo() 
                        有参函数:
                        def bar(x,y):
                        print('in the bar')

                        bar(1,2)
                        空函数:
                        def func():
                        pass
        空函数的应用示例:

def put():
pass

def get():
pass

def cd():
pass

def ls():
pass

def auth():
pass
3. Call function:
three forms:
statement form:

def foo():
print('in the foo')

foo()
expression form:

def my_max(x,y)
if x>y:
return x
else:
return y

res = my_max(1,2)
res = 10 * my_max(1,2)
as a parameter of another function: my_max(1,my_max(2,3)): #First
compare the maximum value of 2 and 3, and then compare with 1 compared to;

4. There are three forms of the return value of the function:
no function is returned:

def foo():
print('in the foo')

res = foo()
print(res)

Return a function:

def foo():
return 1
res = foo()
print(res)

returns multiple:

def foo():
return 1,'s',[1,2,3]
res = foo()
print(res)

5. Parameters of the function:

def func(x,y): #The formal parameter only occupies the memory space when it is called, and the memory space is released when the call ends;
print(x)
print(y)

func(1,2) #Actual parameters actually occupy memory space;

def func(x,y):
if type(x) is int and type(y) is int:
return x+y

func(1,'a')

def func(x,y): #Use documentation comments to remind the user to enter parameter types;
'''
'''
return x+y

func(1,'a')

def func(x:int,y:int)->int: #Just a hint;
pass
print(func. annotations )

From the point of view of arguments,
pass values ​​by position:
def foo(x,y):
print(x,y)
foo(1,2)

Passing parameters by keyword: key = value
def foo(x,y):
print(x,y)
foo(y=2,x=1) #Advantage: No need to input parameters by position;

For the same formal parameter, either by position or by keyword as the formal parameter value; the keyword parameter must be written to the right of the positional parameter; for
example: foo(1,x=1,y=2) This method will report an error;

From the perspective of formal parameters: positional parameters, default parameters,
variable-length parameters *args; **kwargs;
formal parameters defined by position:
def foo(x,y,z): #positional parameters; that is, parameters that must be passed ;
print(x)
print(y)
print(z)

foo(1,2,3)
foo(y=2,x=1,z=3)

Formal parameters according to default parameters:
def foo(x,y=1): #y is the default parameter; but it can also be assigned; it is recommended not to use variable types such as lists or dictionaries for default parameters; it must be placed after the default parameter;
print (x)
print(y)
foo(1)

According to the formal parameters of variable-length parameters:
def foo(x,y=1,*args): #Variable-length parameters must be placed after positional parameters and default parameters; positional parameters are generally not used in this case;
print(x)
print(y)
print(args)

foo(1,2,3,4,54,6,3,y=2) #wrong
foo(1,2,3,4,54,y=2,3,5) #wrong
foo(1,2, 3,4,54,6,3) #Yes

def foo(x,y,args):
print(x)
print(y)
print(
args)

l=['a','b']
foo(1,2, 1) # The form of args is equal to 1,2,3,4,5 unpacking;

('a','b')

def foo(x,y,z):
print(x)
print(y)
print(z)

foo(1,2,3)

l=[1,2,3]
foo(*l)

def foo(x,**kwargs):
print(x)
print(kwargs)

foo(1,y=3,z=2)
dic = {'a':1,'b':2}
foo(1,**dic) #foo(1,a=1,b=2)

def foo(x,y,z):
print(x,y,z)

foo( {'a':1,'b':2,'c':3}) #foo(a=1,b=2,c=3), change a,b,c to x,y ,z
foo(
{'x':1,'y':2,'z':3})

Note: Positional parameters -> default parameters, *args, * kwargs
1.
args is equivalent to expanding and writing by position;
2. **kwargs is equivalent to writing kwargs by keyword;

Functions can be passed as data

def func()
print('in the fun')

fl = fun
fl()

Functions can take arguments: Higher-order functions:
def foo(x):
x()
foo(func)

The return value can be a function:
##########################################

Can be an element of container type:
func_dic={
'func':func
}

func_dic ['func'] ()

Can be used as the return value of a function
"""
When a function accepts one or more functions as input or the output (returned) value of a function is a function, we call such a function a higher-order function
"""
def foo():
print(" return value")

def bar(func):
return func
f = bar(foo)
f()
bar(foo())
function nesting
1. Nested calls
# The meaning of nested functions is equivalent to splitting a large requirement into multiple small requirements and then Combined together, take the following example
max2 function to do two value sizes. For example, if you want to do more than 10, 100, you need to combine
def max2(x,y) in max4:
return x if x > y else y

def max4(a,b,c,d):
res1=max2(a,b)
res2=max2(res1,c)
res3=max2(res2,d)
return res3

print(max4(10,99,31,22))

2. Function nested definition
#The nested definition of the function
def f1():#The first step is to enter the f1 function
def f2():#The second f1 function body contains the f2 function declaration
print('from f2')
def f3 ():#The fourth part of the f2 function body has the f3 function declaration
print('from f3')
f3()#The fifth part of the f2 function body runs the f3 function
f2()#The third part of the f1 function body runs the f2 content

f1 ()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326338252&siteId=291194637