Familiar with Python variables and parameters, and familiar with python Variables
So what are the differences and connections between the two? I searched on the Internet and found many sayings, although similar, but it seems that only the following section is highly abstract from the Microsoft website and has far-reaching significance. I excerpted it and read it as an official reader. Is it understandable? Although it is for VB, it is just as enlightening.
Copy codeThe Code is as follows:
Differences between parameters and variables (Visual Basic)
In most cases, the process must contain information about the calling environment. The process of executing duplicate or shared tasks uses different information for each call. This information contains the variables, constants, and expressions that are passed to each call process.
To pass this information to the process, first define a form parameter, and then call the code to pass a real parameter to the defined form parameter. You can use parameters as a parking space and real parameters as a car. Just as a parking space can park different cars at different times, the calling code can pass different real parameters to the same parameter during each call process.
The parameter indicates a value, which you want to pass when calling it.
When defining a Function or Sub process, you must specify a list of parameters in parentheses that follow the process name. For each parameter, you can specify the name, data type, and input mechanism (ByVal (Visual Basic) or ByRef (Visual Basic )). You can also specify that a specific parameter is optional. This means that the call code does not have to pass its value.
Each parameter name can be used as a local variable in the process. The usage of the parameter name is the same as that of any other variables.
The real parameter indicates the value passed to the process parameter when you call the process. The Call Code provides parameters during the call process.
When calling a Function or Sub process, you must include a list of real parameters in parentheses that follow the process name. Each real parameter corresponds to the parameter in the same position in this list.
Unlike the Parameter definition, the real parameter has no name. Each real parameter is an expression that contains zero or more variables, constants, and text. The data type of the evaluated expression should usually match the data type defined for the corresponding parameter, and the expression value must be convertible to this parameter type under any circumstances.
If you read the citation, you will find several keywords: parameters, variables, parameters, and real parameters. I wanted to figure out the parameters and variables, and two other things emerged, which is even more confusing. Please be careful. This citation is a bit redundant. However, the reason why it is cited is to broaden the horizons of the column spaces. In the programming industry, there are many terms for similar things. You don't have to worry about this next time. You have heard it.
In Python, it is not that complicated.
After reading the dizzy quotes above, you can see the following code again, and then you will be enlightened.
Copy codeThe Code is as follows:
>>> Def add (x): # x is a parameter.
... A = 10 # a is a variable
... Return a + x
...
>>> X = 3 # x is a variable, except for a function.
>>> Add (x) # Here, x is a parameter, but it is passed by the preceding variable x to object 3.
13
>>> Add (3) # merge the above process
13
So far, it is clear whether the officer is a little bit clear. Of course, please kindly advise me if I have not correctly stated or understood the mistakes. Thank you very much.
Global and local variables
The following is a piece of code. Note that there is a function funcx () in this Code. There is a variable x = 9 in this function, and there is a variable x = 2 in front of the function.
Copy codeThe Code is as follows:
X = 2
Def funcx ():
X = 9
Print "this x is in the funcx: -->", x
Funcx ()
Print "--------------------------"
Print "this x is out of funcx: -->", x
So what are the output results of this code? See:
Copy codeThe Code is as follows:
This x is in the funcx: --> 9
--------------------------
This x is out of funcx: --> 2
From the output, run funcx () and output the variable x = 9 in funcx (). then execute the last line in the code, print "this x is out of funcx: --> ", x
Note that the first x outputs the variable x inside the function, and the other x outputs the variable x outside the function. The two variables do not affect each other, although both are x. From this we can see that the two X variables play a role in their respective fields, so such variables are called local variables.
When there is a local variable, there are all corresponding variables. In Chinese, all variables seem to be ambiguous. Thanks to the rich Chinese language, another term is used: global variables.
Copy codeThe Code is as follows:
X = 2
Def funcx ():
Global x
X = 9
Print "this x is in the funcx: -->", x
Funcx ()
Print "--------------------------"
Print "this x is out of funcx: -->", x
The difference between the above two codes is that the latter has a global x in the function, which means that x is a global variable, that is to say, this x is the same as the x outside the function, and then the reference object of x is changed to 9 through x = 9. Therefore, the following result is displayed.
This x is in the funcx: --> 9
--------------------------
This x is out of funcx: --> 9
It seems that global variables are powerful and can take charge of internal and external functions. However, you should be careful when using this item, because it is often prone to variable change disorder. It must be noted in the program.
Number of Uncertain Parameters
When designing a function, sometimes we can confirm the number of parameters. For example, a function used to calculate the circular area requires the radius (π r ^ 2 ), the parameters of this function are correct.
However, the world is not always so simple or definite, but uncertainty is a constant in the world. If you understand quantum mechanics, something that many people have never heard of, you can understand the real uncertainty. Of course, we don't need to study quantum mechanics, and we can realize that the world is full of uncertainty. Isn't it? This is not uncertainty?
Since there are a lot of uncertainties, the number of function parameters is also of course there is uncertainty. How can a function solve this problem? Python solves the uncertainty of the number of parameters in this way:
Copy codeThe Code is as follows:
Def add (x, * arg ):
Print x # output parameter x value
Result = x
Print arg # output value obtained through * arg
For I in arg:
Result + = I
Return result
Print add (, 9) # The number of parameters assigned to a function is more than two
After running this code, the following results are obtained:
1 # This is the first print in the function body. The value of parameter x is 1.
(2, 3, 4, 5, 6, 7, 8, 9) # This is the second print in the function. The arg parameter obtains a tuple.
45 # final calculation result
The output above is not very user-friendly. If you do not follow the original function, you do not know what is printed on each line. Blame yourself.
As shown in the preceding example, if too many parameters are input, all other parameters are passed to the parameter (variable) arg in the form of a tuple through * arg. Please note that here I use a fuzzy word: parameter (variable), which means that arg is a parameter in the function header before the data is passed in, when it is used in a function statement, it is a variable. That is, in many cases, the parameters and variables in the function do not need to be so differentiated and the truth is that you only need to know the channel through which the object is transmitted and the target of the object.
To make it clearer that the args (names can be different, but the symbols must be), we can use the following simple function to demonstrate it:
Copy codeThe Code is as follows:
>>> Def foo (* args ):
... Print args # print the object obtained through this parameter
...
>>># The following example shows the result of passing in different values through the * args parameter.
>>> Foo (1, 2, 3)
(1, 2, 3)
>>> Foo ("qiwsir", "qiwsir. github. io", "python ")
('Qiwsir ', 'qiwsir. github. io', 'python ')
>>> Foo ("qiwsir", 307, ["qiwsir", 2], {"name": "qiwsir", "lang": "python "})
('Qiwsir ', 307, ['qiwsir', 2], {'lang ': 'python', 'name': 'qiwsir '})
No matter what it is, it is all pushed into the tuple.
In addition to receiving multiple values using parameters in the form of * args, you can also receive values in the form of ** kargs, but this time it is a bit different:
Copy codeThe Code is as follows:
>>> Def foo (** kargs ):
... Print kargs
...
>>> Foo (a = 1, B = 2, c = 3) # observe the Assignment Method and the printed result.
{'A': 1, 'C': 3, 'B': 2}
If foo (1, 2, 3) is used this time, what will happen?
Copy codeThe Code is as follows:
>>> Foo (1, 2, 3)
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: foo () takes exactly 0 arguments (3 given)
I may think about it here, isn't it uncertainty? I don't know how the parameter will pass the value. It's easy to combine the above methods.
Copy codeThe Code is as follows:
>>> Def foo (x, y, z, * args, ** kargs ):
... Print x
... Print y
... Print z
... Print args
... Print kargs
...
>>> Foo ('qiwsir ', 2, "python ")
Qiwsir
2
Python
()
{}
>>> Foo (1, 2, 3, 4, 5)
1
2
3
(4, 5)
{}
>>> Foo (1, 2, 3, 4, 5, name = "qiwsir ")
1
2
3
(4, 5)
{'Name': 'qiwsir '}
This is good enough to meet various parameter requirements.