Function acquaintance: definition, call, return values, parameters

The definition of a function call, the return value

1. The role of function: a package to reduce duplication of code

2. The format of the function

def keyword defines a function
def function name ():

Indent function body (block)

def func():
    print('我')
    print('爱')
    print('你')
    return '1314'  #返回值,将内容返回给调用者
              #不写return或写了return不写值返回None
msg = func()      #函数名() -- 调用
print(msg)

a plurality of variable return returns, returns the ancestral form, what is returned is not limited

When performing the function to return the time is over, return the following code does not execute, and terminate the function.

return can not terminate the loop, only the termination function

Parameter Two function

2.1 positional parameters

def func(addr,addr1):    #形参
    print(f'我在{addr}上网')
    print(f'你在{addr1}睡觉') 
func('网吧','沙发')    #实参 

Parameter is defined parameters, parameters of the call is the argument, the argument from the process parameter called mass parameter

Parameter is a variable name, the argument is the value that is assigned mass participation

And the number of formal parameters correspond to arguments

2.2 Keyword parameters (argument angle)

def func(addr1,addr):    
    print(f'我在{addr}上网')
    print(f'你在{addr1}睡觉') 
func(addr = '网吧',addr1 = '沙发')

2.3 mixing parameters

def func(addr,addr1):    
    print(f'我在{addr}上网')
    print(f'你在{addr1}睡觉') 
func('网吧',addr1 = '沙发')

Note: when using the mixing parameters, keyword parameters must be in the back position parameters

2.4 The default value of the parameter (parameter angle)

def func(addr,addr1= '沙发'):  #默认值参数  
    print(f'我在{addr}上网')
    print(f'你在{addr1}睡觉') 
func('网吧',addr1 = '床上') #下面的addr1的值会覆盖上面的值

NOTE: When using the mixing parameters, the default value of the parameter must be behind the position parameters

Most of the default values ​​for the parameters passed in the general parameters are the same

2.5 dynamic parameters

1. Dynamic Universal position parameter variable name * (any number of receiving) position parameters

def eat(a,b,*args,c='白菜'): #聚合打包
    print('我想吃',a,b,args,c) #元祖形式(接受的是位置参数)
eat('猪肉','粉条','豆腐','大葱')

Results:
I want to eat pork vermicelli ( 'tofu', 'onion') cabbage

def eat(a,b,*args,c='白菜'): 
    print('我想吃',a,b,*args,c) #打散
eat('猪肉','粉条','豆腐','大葱')

Results:
I want to eat pork, cabbage, green onions tofu noodles

Dynamic parameter must be behind the position parameters

Order parameter: the parameter position, the position of the dynamic parameters, the default parameters

2. Dynamic keyword arguments variable name **

def func(**kwargs):  
    print(kwargs)  # 字典形式(接收的是关键字参数)   
func(a=1, b=2, c=3)

result:

{'a': 1, 'b': 2, 'c': 3}

def func(**kwargs):  
    print(*kwargs)  # 得到的是字典的键
func(a=1, b=2, c=3)

result:

a b c

def func(a,b,*args,c=1,**kwargs):
    print(a,b,c,args,kwargs)
func(1,2,3,4,5,6,7,8,9,c=55,d=1,f=3)

result:

1 2 55 (3, 4, 5, 6, 7, 8, 9) {'d': 1, 'f': 3}

Position parameter> * args (dynamic position parameter)> The default value of the parameter> ** kwargs (dynamic default parameters)

Transfer list

lst = [1,4,7]
def func(*args):
    print(args)
func(*lst)    #打散 去壳
#在实参的位置上用*将lst(可迭代对象)按照顺序打散在#形参的位置上用*把收到的参数组合成一个元祖

Biography Dictionary

dic = {'a':1,'b':2}
def func(**kwargs)
    print(kwargs)
func(**dic)  #字典的键必须是字符串

Note 3 to function

In the function "" "Enter" ""

函数名.__doc__ 查看函数的注释 

Guess you like

Origin www.cnblogs.com/lav3nder/p/11801498.html