Function Namespace Exercise

eg1:  Can args and kwargs be printed, what is the result of printing?

def func(*args,**kwargs):
    print(args)
    print(kwargs)
func (1,2,3,4, sex = 'ols', male = 'chuchu')

print result: tuples and dictionaries

Interpretation: The args print result is the tuple, and kwargs prints the result dictionary.

eg2: Positional parameters, what is the order of parameters received by args?

def func(a,b,c,*args):
    print(a)
    print(b)
    print(c)
    print(args,type(args))
func (1,2, 'alex')

print result

Interpretation: First print the positional parameters, then print the args. If you put *args first, then a, b, and c cannot receive the value and report an error.

eg3 What is the order of receiving parameters for positional parameters, args, default parameters?

def func(a,b,c,sex='nan',*args):
    print(a)
    print(b)
    print(c)
    print(sex)
    print(args,type(args))
func(1,2,'alex','wusir','ritian')

print result

Interpretation: first position, then args, and finally default parameters, otherwise the default parameters are overwritten, like the one above, sex='nan' is overwritten directly and cannot be printed.

   positional arguments, *args, default arguments, **kwargs

 

Magic operation:

def func(*args):
    print(args)
li = [1,2,3]
l2 = [4,5,6]
func(li) #Output result: ([1, 2, 3],)
func(*li) #Output result: (1, 2, 3)
func(*li,l2) #Output result: (1, 2, 3, [4, 5, 6])
func(*li,*l2) #Output result: (1, 2, 3, 4, 5, 6)

 

 

 

 

 

 

Guess you like

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