The basic functions used python-

I. Introduction argument between Form

Parameter: the parameters defined in stage called the formal parameters defined function, referred to as parameter, corresponding to the variable name

def func(x, y):  # 取x=1,y=2
    print(x, y)

Argument: the incoming call function called actual parameter value stage, referred to as the argument, equivalent to the value of variable

func(1,2)

Relations between Form argument:

In the call phase, the argument (variable value) will be bound to the parameter (variable names)

This binding relationship can only be used in the function body

Real participation parameter binding relationship in the function call to take effect after the end of the function call unbind

Value argument is passed, but the value may be in the form

# 形式一
func(1,2)
# 形式二
a=1
b=2
func(a,b)
# 形式三
func(int('1'),2)
func(func1(1,2,),func2(2,3),333)

Second, the parameters of the parameter and the specific use

1. Location parameter

Called the position parameter according to the parameter defined sequentially from left to right

Location parameter: function definition stage, left to right, the direct definition of "variable name"

Features: must be transferred value, not one more nor one less

# 示例
def func(x,y):
    print(x,y)
func(1,2,3) # output:func() takes 2 positional arguments but 3 were given
func(1,) # output:func() missing 1 required positional argument: 'y'

Location argument: the call phase function, in accordance with the order and some from the left to the incoming value

Features: the order of one-parameter

def func(x,y):
    print(x,y)
func(1,2)   # output:1 2
func(2,1)   # output:2 1

2. Keyword parameters

关键字实参:在函数调用阶段,按照key=value的形式传入的值

Features: by name to pass a parameter value, the reference sequence can be completely

def func(x,y):
    print(x,y)
func(y=2,x=1)   # output:1 2
func(1,2)   # output:1 2

Mix, emphasizing

# 1、位置实参必须放在关键字实参前
func(1,y=2) # output:1 2
func(y=2,1) # output:positional argument follows keyword argument
# 2、不能能为同一个形参重复传值
func(1,y=2,x=3) # output:func() got multiple values for argument 'x'
func(1,2,x=3,y=4)   # output:func() got multiple values for argument 'x'

3. Default parameters

Default parameter: function definition phase, has been assigned a parameter, called the default parameters

Features: During the definition phase has already been assigned, meaning that the call can not assign a value to stage

def func(x,y=3):
    print(x,y)

func(x=1)   # output:1 3
func(x=1,y=44444)   # output:1 44444

# 示例
def register(name,age,gender='男'):
    print(name,age,gender)

register('三炮',18)   # output:三炮 18 男
register('二炮',19)   # output:二炮 19 男
register('大炮',19)   # output:大炮 19 男
register('没炮',19,'女')   # ouyput:没炮 19 女

Location shaped participate default parameter mix, emphasizing:

# 1、位置形参必须在默认形参的左边
def func(y=2,x):
    pass

# 2、默认参数的值是在函数定义阶段被赋值的,准确地说被赋予的是值的内存地址
# 示例1:
m=2
def func(x,y=m): # y=>2的内存地址
    print(x,y)
    
m=3333333333333333333
func(1)     # output:1 2
# 示例2:
m = [111111, ]
def func(x, y=m): # y=>[111111, ]的内存地址
    print(x, y)

m.append(3333333)
func(1) # output:1 [111111, 3333333]

# 3、虽然默认值可以被指定为任意数据类型,但是不推荐使用可变类型函数最理想的状态:函数的调用只跟函数本身有关系,不外界代码的影响
# 示例:
m = [111111, ]
def func(x, y=m):
    print(x, y)

m.append(3333333)
m.append(444444)
m.append(5555)
func(1) # output:1 [111111, 3333333, 444444, 5555]
func(2) # output:2 [111111, 3333333, 444444, 5555]
func(3) # output:3 [111111, 3333333, 444444, 5555]

4. The variable length parameters - (*) and (**)

Position parameter 1) variable length

# 1.*形参名:用来接收溢出的位置实参,溢出的位置实参会被*保存成元组的格式然后赋值紧跟其后的形参名*后跟的可以是任意名字,但是约定俗成应该是args
# 示例1
def func(x,y,*z): # z =(3,4,5,6)
    print(x,y,z)
func(1,2,3,4,5,6)   # output:1 2 (3, 4, 5, 6)
# 示例2
def my_sum(*args):
    res=0
    for item in args:
        res+=item
    return res

res=my_sum(1,2,3,4,)
print(res)  # output:10

# 2.*可以用在实参中,实参中带*,先*后的值打散成位置实参
def func(x,y,z):
    print(x,y,z)

func(*[11,22,33]) # 相当于func(11,22,33),output:11 22 33
func(*[11,22]) # 相当于func(11,22),output:func() missing 1 required positional argument: 'z'

# 3.形参与实参中都带*
def func(x,y,*args):    # args=(3,4,5,6)
    print(x,y,args)

func(1,2,[3,4,5,6])
func(1,2,*[3,4,5,6])    # func(1,2,3,4,5,6)
func(*'hello')  # func('h','e','l','l','o')

Keyword parameters 2) variable length

# 1.**形参名:用来接收溢出的关键字实参,**会将溢出的关键字实参保存成字典格式,然后赋值给紧跟其后的形参名**后跟的可以是任意名字,但是约定俗成应该是kwargs
def func(x,y,**kwargs):
    print(x,y,kwargs)
    
func(1,y=2,a=1,b=2,c=3)

# 2.**可以用在实参中(**后跟的只能是字典),实参中带**,先**后的值打散成关键字实参
def func(x,y,z):
    print(x,y,z)

func(*{'x':1,'y':2,'z':3}) # func('x','y','z')
func(**{'x':1,'y':2,'z':3}) # func(x=1,y=2,z=3)

# 错误
# func(**{'x':1,'y':2,}) # func(x=1,y=2)
# func(**{'x':1,'a':2,'z':3}) # func(x=1,a=2,z=3)


# 3.形参与实参中都带**
def func(x,y,**kwargs):
    print(x,y,kwargs)

func(y=222,x=111,a=333,b=444)
func(**{'y':222,'x':111,'a':333,'b':4444})

3) mix

# 混用*与**:*args必须在**kwargs之前
def func(x,*args,**kwargs):
    print(args)
    print(kwargs)

func(1,2,3,4,5,6,7,8,x=1,y=2,z=3)
# 示例
def index(x,y,z):
    print('index=>>> ',x,y,z)

def wrapper(*args,**kwargs): #args=(1,) kwargs={'z':3,'y':2}
    index(*args,**kwargs)
    # index(*(1,),**{'z':3,'y':2})
    # index(1,z=3,y=2)

wrapper(1,z=3,y=2) # 为wrapper传递的参数是给index用的
# 原格式---》汇总-----》打回原形

Guess you like

Origin www.cnblogs.com/zhuyouai/p/12520596.html
Recommended