Python parameters

When the function is defined

In Python, a function can have the following parameters when it is defined:

Positional argument

Parameters that must be passed when calling the function.

>>> def say(word):
...     print(word)
...
>>> say()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: say() missing 1 required positional argument: 'word'
>>> say("Hello")
Hello
>>>

In the above code, say()a mandatory parameter is specified word. If this parameter is not passed in when calling, an error will be reported and a missing wordparameter will be prompted.

default argument

Default parameters are parameters with default values ​​specified when the function is declared. If no value is passed in when the function is called, the default value is used (this can reduce the number of passed parameters in some cases).

>>> def say(word, language="English"):
...     print("Saying " + word + "in " + language)
...
>>> say("Hello")
Saying Helloin English
>>> def say(word, language="English"):
...     print("Saying " + word + " in " + language)
...
...
>>> say("Hello")
Saying Hello in English
>>> say("Hello", "Chinese")
Saying Hello in Chinese

There are a few points to note when using default parameters:
1. Default parameters need to be defined after the mandatory parameters
2. The default value of default parameters is preferably immutable
The first point is easy to understand, default parameters can be passed or not, so they cannot be defined in Before the required parameters:

>>> def say(language="English", word):
...     print(word)
  File "<console>", line 1
SyntaxError: non-default argument follows default argument

Regarding the second point, since the default parameter is assigned when the function is defined, if the default parameter is assigned to a mutable object, such as an instance of the List Dict class, etc., the change to the default parameter will be in all places where the function is called. shared.

>>> def f(a, L=[]):
...     L.append(a)
...     return L
...
>>> print(f(1))
[1]
>>> print(f(2))
[1, 2]
>>> print(f(3))
[1, 2, 3]

The official website also provides a solution:

>>> def f(a, L=None):
...     if(L is None):
...             L = []
...             L.append(a)
...     return L
...
>>> print(f(1))
[1]
>>> print(f(2))
[2]
>>> print(f(3))
[3]

Arbitrary argument Lists

If we do not strictly require the number of incoming parameters when we define the function (the number of parameters can be 0 to n), we can use variable parameters *argto argrepresent the incoming parameter array.

>>> def say(*words):
...     for i in words:
...             print(i)
...
>>> say("Hello", "World")
Hello
World

Keyword argument

If our function accepts an indeterminate number of key-value pairs as parameters, it can be used **argto argrepresent the incoming dict (0 to n key-value pairs).

>>> def say(**args):
...     print(args["word"] + " in " + args["language"])
...
>>> say(word="Hello", language="English")
Hello in English

parameter combination

If a function needs to receive the above parameters at the same time, it should be noted that the declaration order of the above parameters is: mandatory parameters, default parameters, array parameters, key-value pair parameters.

When the function is called

In Python, when calling a function, we can use it together * **to simplify the input of parameters.
If the parameters that need to be passed to a function are already contained in an array, we can use *to pass in this array.

>>> say(word="Hello", language="English")
Hello in English
>>> def say(word, language):
...     print(word + " in " + language)
...
>>> str = ["Hello", "English"]
>>> say(*str)
Hello in English

If the key-value pairs that need to be passed into a function are already stored in a dict, we can use **to pass in this dict

>>> def say(word, language):
...     print(word + language)
...
>>> dict = {"word":"Hello", "language":"English"}
>>> say(**dict)
HelloEnglish

Guess you like

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