In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the difference between single underscore and double underscore in python variables? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. definition of variables
In Python, there are several ways to define variables:
Xx: public variable
_ xx: pre-order underscores, privatized properties or methods. In general, the variable name _ xx is considered "private" and cannot be used outside the module or class. It is a good habit to use _ xx to represent a variable when it is private. Class objects and subclasses can be accessed, which is not completely private, but by convention. This means that you do not want this variable to be called directly from the outside.
_ _ xx: preceded by double underscores, privatized properties or methods, cannot be accessed directly from the outside (the name is renamed so it cannot be accessed, only the class itself is allowed to access it. Not even subclasses)
_ _ xx__: double underscores before and after the system-defined name (this is a powerful magic method in python), because the variable name _ _ xxx__ has a special meaning to Python, and this naming style should be avoided for ordinary variables.
Xx_: rear order underline to avoid conflicts with Python keywords
As shown in the following example, I have defined three attributes, x _ _ x and _ _ x, in the test class, and created a test class object t to access these three attributes. _ _ x cannot be accessed.
Learning is never a personal thing. If you want to have a partner who supervises each other, you need to study python or those who are interested in learning python can reply to Xiaobian "Learning" to get information and study together.
You can use the command dir to view the properties and methods in t. The name of _ x has been restructured to "_ test__x". If you have to access it externally, you can use t._test__x to access it.
There are no real public and private variables in python. Python just makes a change to the variable name, and it is also accessible externally, which is pseudo-private.
Note: although private variables can be accessed through _ class name _ _ variable name, it is strongly recommended that you do not do so!
Second, using different methods to import modules, differences in the use of private variables in the module
Whether the private properties and methods in the module can be used after importing the module using different methods, there are two situations
You cannot import private variables by using from somemodule import *. The result is as follows:
In the case of the import somemodule import module, you can import and use private properties and methods
Third, the method of manipulating private attributes in a class
If you want to operate on private properties in a class, there are two ways:
Note:
1. In the constructor _ _ init__, it is _ x, not x. You cannot access it directly with x.
2. Why is it _ x instead of x? this is a private variable in python (although there are no public and private variables in python, that is to say, it is pseudo-private). In python, if a variable does not want to be accessed directly from the outside, it is usually preceded by an underscore to remind you not to access it directly (in fact, it can be accessed through self._x. If you do not want to be accessed, you can also use double underscore _ x. Also can not directly access) so provide functions such as get_x for users to access
3. Why do we use pseudo-private variables such as _ x, because when we use property, we call the method as a property, usually writing x=property (...), so that the call is more clear; but if the variable in the constructor also uses x, it will conflict with the variable in property, so we usually write _ x
After reading the above, have you mastered the difference between single underscore and double underscore in python variables? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.