Two major categories of function parametersFormal parameters are the parameters written in parentheses during the function definition phase. Actual parameters are the parameters passed in parentheses during the function call phase. The relationship between formal parameters and actual parameters Formal parameters can be regarded as variable names, and actual parameters can be regarded as variable values. They are temporarily bound during the function call phase and disconnected when the function is finished. There are many forms of variable names and actual parameters (grasp the core data value) Positional parametersPositional parameters are the positional parameters filled in from left to right. Variable name positional parameters are filled in from left to right during the function definition phase. Data values are filled in from left to right during the function call phase. Keyword arguments (can break positional order) During the function call phase, the value is passed in the form of parameter name = data value. 1. Positional parameters and positional actual parameters are bound according to the corresponding position during the function call phase. 2. When binding positional parameters, one more or one less is not allowed. Key points: The simpler the format, the earlier it is, and the more complex it is, the later it is. Variable length parameters1. The function can run normally no matter how many positional parameters are passed in. Variable length parameters def func(x,y,*a): print(x,y,a) func() # () func(1) # (1,) func(1, 2, 3, 4, 5, 6, 7) # (1, 2, 3, 4, 5, 6, 7) func(1,2) # 1 2 () func(1,2,3,4,5,6,7,8,9) # 1 2 (3, 4, 5, 6, 7, 8, 9) func(1,2) # 1 2 (3, 4, 5, 6, 7, 8, 9) * is used in the parameter to receive extra positional parameters and organize them into tuples and assign them to the variable name after * 2. The function can run normally no matter how many keywords are passed in def index(x, y, **b): print(x, y, b) index() # {} index(a=1,b=2,c=3,d=4) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} index(y=2, x=1) # 1 2 {} index(y=2, x=1, u=222, k=111, l=444) # 1 2 {'u': 222, 'k': 111, 'l': 444} **In the parameter, it is used to receive extra keyword parameters and organize them into a dictionary to assign to the variable name behind it. *Using it will break up the data in the list and tuple. **Using it will break up the key-value pairs of the dictionary into keyword parameters and pass them in. Namespace1. Built-in namespace print() len() 2. Global namespace Code written in the top grid of the py file name = 'jason' # name global def func(): # func global pass if 1: a = 123 # a global for i in range(10): print(i) # i global while True: a = 123 # a global 3. Local namespace After the function body code is run, the local namespace is generated. SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: User experience of portal website redesign
>>: Docker installs ClickHouse and initializes data testing
Table of contents cache Cache location classifica...
uninstall First, confirm whether it has been inst...
Form provides two ways of data transmission - get ...
Table of contents MySQL query tree structure 1. A...
Table of contents Preface 1.1 Function 1.2 How to...
Today I made a Spring Festival gold coin red enve...
Preface Nginx is a lightweight HTTP server that u...
Preface I recently made a fireworks animation, wh...
Table of contents 1. concat() 2. join() 3. push()...
Start a new project This article mainly records t...
How to install MySQL 5.7 in Ubuntu 16.04? Install...
On Linux, bash is adopted as the standard, which ...
NTP is a TCP/IP protocol for synchronizing time o...
<br />Structure and hierarchy reduce complex...
Table of contents Asynchronous traversal Asynchro...