Common underscores in Python

Common underscore meanings in python

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 befrom module import _XXXimported, _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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325316632&siteId=291194637