程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python detailed explanation of *args and * * kwargs parameters (all)

編輯:Python

Catalog

  • Preface
  • 1. Location parameters and keyword parameters
  • 2. *args
  • 3. **kwargs
  • 4. *args and **kwargs combination

Preface

See these two parameters in the company , adopt logger.info You can see the desired result from the output of
Curious, I have a deep understanding of these two parameters

Some basic commonalities are :

  • Both parameters are a variable parameter
  • If the parameter is uncertain, these two parameters can be combined to replace

To put it simply args(arguments): Positional arguments .kwargs(key arguments): Key parameters

1. Location parameters and keyword parameters

Science Popularization python Parameter type ( Like other languages, there are formal parameters 、 Parameters, etc ):

  • Formal parameter, actual parameter ( Similar to other languages ): Defined function ( Shape parameter ), Call function ( Actual parameters )
  • Positional arguments : The position and definition meaning of the parameters passed in by the function and the parameters of the actual function should be consistent ( The position of the argument is consistent with that of the formal parameter , Assign values one by one according to the order of formal parameter declarations )
# Custom function 
def func(a, b):
print(a + b)
# Actual call call , The positions correspond one by one 
func(1,1)
  • Key parameters : Call function (key=value) In the form of key value pairs ( The order of arguments doesn't matter ). If the parameter has *args and **kwargs, Should be maintained (*args,**kwargs)
# Custom function 
def func(a, b):
print(a + b)
# Actual call call , Keywords are transmitted in the form of dictionaries 
func(a=1, b=1)

If both parameters ( Location parameters and keyword parameters ) A mixture of , Keyword parameter must be after positional parameter
As for why we should be in the back , By combining the position parameter and the keyword parameter principle :
Positional arguments : The positions of arguments and formal parameters are consistent , Assign values one by one according to the order of formal parameter declarations , Otherwise it will go wrong
Key parameters : Call the function with formal parameters = Arguments ,( The order of arguments doesn't matter )
See the complete example for details :

# Custom function 
def func(a, b):
print(a + b)
# A mixture of , Call the output correctly 
func(1, b=1)
# A mixture of , Error call , Unable to output 
func(a=1, 1)

What parameters need to be passed in , And what kind of
See my previous article for details :
python Detailed analysis of data types ( The attached code )

2. *args

If... Is added before the parameter name *, Represents that this parameter is a variable parameter

The knowledge points are as follows :

  • Reference time , Accept any position parameter
  • The parameters will be assembled as a whole Tuples For storage , The whole is assigned to the variable named *args

Through the popular science of the above parameters ,*args For position parameters
Specific examples are clear :

# Custom function 
def func(*args):
print(type(args))
# Call function , Pass in 2 A numerical 
func(1, 2) # The output is <class 'tuple'> , Store in tuple format 

Now that you know how to store in tuple format
You can customize a tuple and pass in parameters

# Define tuples 
tuple = (1, 2)
# Call function 
func(*tuple)

3. **kwargs

The knowledge points are as follows :

  • The parameter , Accept any keyword parameter
  • The parameters will be assembled as a whole Dictionaries For storage , The whole is assigned to the variable named **kwargs

Specific examples are as follows :

# Custom function 
def func(**kwargs):
print(type(kwargs))
# Call function , Pass in 2 A numerical 
func(a=1 , b=2) # The output is <class 'dict'> , Store in dictionary format 

Now that we know that it is the format storage of the dictionary , You can customize a dictionary

# Definition dictionary 
dict = {

'a': 1,
'b': 2
}
# Call function 
func(**dict)

4. *args and **kwargs combination

As we know above, the keyword parameter should follow the position parameter
Specific examples are as follows :

# Custom function , contain *args and **kwargs
def func(argument, *args, **kwargs):
print(argument) # Output 1
print(args) # Output 2
print(kwargs) # Output 3
# Call function , Pass in argument、args as well as kwargs Parameters 
func(1, 2, a=3)

We already know that one is a location parameter and the other is a keyword parameter
You can customize the type of a tuple and dictionary :

# Define tuples and dictionary types 
tuple = (1, 2)
dict = {

'a': 1,
'b': 2
}
# Call function , Pass in the parameter 
# Note the order of positional and keyword parameters 
func(*tuple , **dict)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved