In Python, underscores are often used to modify variables, which usually appear as prefixes or suffixes of variables. The modified variables generally have special usage:
-
_XXX : cannot be
from module import _XXX
imported, _XXX in this case is called a protected variable, only class objects and subclass objects can access these variables. -
__XXX__ : system-defined name
-
__XXX : Private members in the class, only the object itself can access this member, even subclass objects cannot access this member. But it can be accessed from outside the object
对象名._类名__XXX
. There are no private members in Python in the pure C++ sense.PS: When you need to access a private member, please consider whether you should cancel the privateness of the private member.
-
In interactive mode,
_
represents the last display in the interpreter or the output of the last correct execution of the statement.
Reference "Python Programming" Tsinghua University Press, Dong Fuguo