Home  >  Article  >  Backend Development  >  What is the difference between single underline and double underline in python?

What is the difference between single underline and double underline in python?

silencement
silencementOriginal
2019-05-23 18:11:387979browse

The difference between single underline and double underline in python is: single underline is used to tell other programs that this property or method is private; double underline is used to define instances, class variables, global variables, and instances of private classes. variables etc.

What is the difference between single underline and double underline in python?

Single underscore

Starting a method or attribute in a class with a single underscore tells other programs that this The property or method is private. Yet there is nothing special about the name.

(Recommended tutorial: python video tutorial)

Double underline

any The __spam form (starting with at least two underscores and ending with at most one underscore) replaces _classname__spam, where classname is the name of the current class. This mangling is done without regard to the syntactic position of the identifier. So it can be used to define private class instances and class variables, methods, variables in the global world, and even variables in the instance. Instances of different classes can be distinguished.

What is the difference between single underline and double underline in python?

Summary:

__foo__: a convention, an internal name in Python, used to distinguish other user-defined names naming to prevent conflicts.

_foo: A convention used to specify that variables are private. A way for programmers to specify private variables.

__foo: This has real meaning: the parser replaces this name with _classname__foo to distinguish it from other classes with the same name.

There is no other form of underscore in Python.

This convention is no different from classes, variables, global variables, etc.

The above is the detailed content of What is the difference between single underline and double underline in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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