Python Explanation of Function Parameters with Asterisk of * or * *

  • 2021-09-11 20:34:58
  • OfStack

1. Parameters with default values

Before you get to the asterisk (*) parameter, let's look at the parameter with the default value. The function is defined as follows:


>> def defaultValueArgs(common, defaultStr = "default", defaultNum = 0):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum) 
 

(1) Call when parameters with default values (defaultStr, defaultNum) are not passed:


>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 

(2) Parameters with default values (defaultStr, defaultNum) can be passed directly (defaultStr in the following example) or written in the form of "argsName = value" (defaultNum in the following example):


>> defaultValueArgs("Test", "Str", defaultNum = 1)
 
Common args Test
Default String Str
Default Number 1
 
>> defaultValueArgs("Test", defaultNum = 1)
 
Common args Test
Default String default
Default Number 1

Note: When defining a function, all parameters after the first parameter with a default value must have a default value, otherwise, the run will report an error.


>> def defaultValueArgs(common, defaultStr = "default", defaultNum):
    print("Common args", common)
    print("Default String", defaultStr)
    print("Default Number", defaultNum)
    
SyntaxError: non-default argument follows default argument
 

2. Function arguments with an asterisk (*)

A function with one argument is defined as follows:


>> def singalStar(common, *rest):
  print("Common args: ", common)
    print("Rest args: ", rest)

(1) Parameters with asterisks (*) are not passed:


>> singalStar("hello")
 
Common args: hello
Rest args: ()

Parameters with asterisks (*) default to 1 empty tuple when they are not passed.

(2) When a parameter with an asterisk (*) passes in multiple values (the number is greater than or equal to the number of parameters when the function is defined):


>> singalStar("hello", "world", 000)
 
Common args: hello
Rest args: ('world', 0)

It is not difficult to see that in the second mode, the asterisk parameter combines multiple received parameters into one tuple.

(3) When we pass the tuple type value directly to the asterisk parameter:


>> singalStar("hello", ("world", 000))
 
Common args: hello
Rest args: (('world', 0),)

At this point, the tuple value passed is one element in the tuple of the asterisk parameter.

(4) If we want to use tuples as parameter values of asterisk parameters, we can add "*" before tuple values.


>> singalStar("hello", *("world", 000))
Common args: hello
Rest args: ('world', 0)

>> singalStar("hello", *("world", 000), "123")
Common args: hello
Rest args: ('world', 0, '123')

3. Function arguments with two asterisks (**)

A function with two asterisks (**) is defined as follows:


>> def doubleStar(common, **double):
    print("Common args: ", common)
    print("Double args: ", double)

(1) Double asterisk (**) parameter does not pass value:


>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 
0

Parameters with double asterisks (**) default to an empty dictionary when they do not pass values.

(2) When the double asterisk (**) parameter passes in multiple parameters (the number is greater than or equal to the number of parameters when the function is defined):


>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 
1

It can be seen that the double asterisk parameter combines the received parameters into one dictionary, but unlike the single asterisk, the default value of "args = value" must be adopted at this time, and the field before "=" becomes the key of the dictionary, and the field after "=" becomes the value of the dictionary.

(3) What should I do if I want to use the dictionary as the parameter value of the asterisk parameter? Similar to the single asterisk parameter, "**" is preceded by the dictionary value, and no value can be added after it.


>> defaultValueArgs("Test")
 
Common args Test
Default String default
Default Number 0
 
2

4. In some cases, single-asterisk and double-asterisk function arguments are used as one:


def singalAndDoubleStar(common, *single, **double):
  print("Common args: ", common)
  print("Single args: ", single)
  print("Double args: ", double)

singalAndDoubleStar("hello")
# Common args: hello
# Single args: ()
# Double args: {}
singalAndDoubleStar("hello", "world", 000)
# Common args: hello
# Single args: ('world', 0)
# Double args: {}
singalAndDoubleStar("hello", "world", 000, {"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", "world", 000, **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}
singalAndDoubleStar("hello", ("world", 000), {"name": "Test", "age": 24})
# Common args: hello
# Single args: (('world', 0), {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), {"name": "Test", "age": 24}) 
# Common args: hello
# Single args: ('world', 0, {'name': 'Test', 'age': 24})
# Double args: {}
singalAndDoubleStar("hello", *("world", 000), **{"name": "Test", "age": 24})
# Common args: hello
# Single args: ('world', 0)
# Double args: {'name': 'Test', 'age': 24}

Related articles: