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 :
To put it simply args(arguments): Positional arguments .kwargs(key arguments): Key parameters
Science Popularization python Parameter type ( Like other languages, there are formal parameters 、 Parameters, etc ):
# Custom function
def func(a, b):
print(a + b)
# Actual call call , The positions correspond one by one
func(1,1)
# 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 )
If... Is added before the parameter name *, Represents that this parameter is a variable parameter
The knowledge points are as follows :
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)
The knowledge points are as follows :
Dictionaries
For storage , The whole is assigned to the variable named **kwargsSpecific 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)
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)