When to use one or two underscores in Python

Good. I think I understood the use of one and two underline headers in Python.

Correct me if I am wrong

  • In the case of one underscore, the underscore prevents the operator from from X import *importing such variables.

  • In the case of two underscores, the variable name is appended with the name of the class to which it belongs to ensure a higher level of "privacy".

Now my question is: why not use only two underscores? When is one underscore preferable (or necessary) over two underscores?

+4
source share
2 answers

: , ( ).

:

" " (, , , ) Python "" Java. , , API //. ( - , , Python, ?), , - (. " , " ).

. - , ( ): . django ( django.utils.functional).

, , , 15 , , .

+8

.

1. Single Underscore

PEP-8:

_single_leading_underscore: " ". . from M import * , .

2. Double Underscore:

Python:

__spam ( , ) _classname__spam, classname - . , , , , , , . . , "private", , , . , ; , , .

+3

Source: https://habr.com/ru/post/1667347/


All Articles