How to understand static variables of classes in Python

anonymity
Release: 2022-05-27 16:12:52
Original
6099 people have browsed it

Python uses function default values ​​to implement function static variables. If it is a static method, you can use the @staticmethod annotation.

How to understand static variables of classes in Python1. Python function default value
The use of Python function default value can provide convenience when writing code when calling the function. Many times we only need to use the default value. Therefore, function default values ​​are used a lot in Python, especially in classes. Default values ​​are generally used in the initialization functions of classes. When using classes, you can easily create classes without passing a bunch of parameters.
As long as "=defalut_value" is added after the function parameter name, the function default value is defined. One thing to note is that parameters with default values ​​must be at the end of the function parameter list. Parameters without default values ​​are not allowed to be placed after parameters with default values, because if you define it that way, the interpreter will not know How to pass parameters.
Let’s look at a sample code first:

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'): return True
if ok in ('n', 'no', 'nop', 'nope'): return False
retries = retries - 1
if retries < 0: raise IOError, &#39;refusenik user&#39;
print complaint
Copy after login

When you call the above function, you can modify the number of retries and the output prompt language. If you are lazy, you don’t need to change anything.
2. Python uses the default value of the function to implement the function of static variables
Static variables are not supported in Python, but we can implement the function of static variables through the default value of the function.
When the default value of the function is a class whose content is variable, the content of the class is variable, but the name of the class does not change. (It is equivalent to the opened memory area not changing, but the contents can change).
This is because the default value of the function in python will only be executed once (like static variables, static variable initialization is also executed once.) This is what they have in common.
Let’s look at the following program fragment:

def f(a, L=[]):
L.append(a)
return L
print f(1)
print f(2)
print f(3)
print f(4,[&#39;x&#39;])
print f(5)
Copy after login

The output result is:

[1]
[1, 2]
[1, 2, 3]
[&#39;x&#39;, 4]
[1, 2, 3, 5]
Copy after login

The previous one is easy to understand, why is "print f(5)" at the end? The output is "[1, 2, 3, 5]"?
This is because the default variable has not been changed when "print f(4,['x'])", because the initialization of the default variable is only executed once (the first call with the default value), the initialization is executed The opened memory area (we can call it the default variable) has not been changed, so the final output result is "[1, 2, 3, 5]".

The above is the detailed content of How to understand static variables of classes in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template