selfboot / AnnotatedShadowSocks

Annotated shadowsocks(python version)
Other
3 stars 1 forks source link

why use single underscore before some variable #7

Open selfboot opened 7 years ago

selfboot commented 7 years ago

In file utils.py and common.py, there are some underscore(_) before some variable. For example:

def _decode_list(data):
    ...
def _decode_dict(data): 
    ...

_ord = ord

_chr = chr

Then, why name them as that?

Single Underscore Before a Name

A single underscore before a name is used to specify that the name is to be treated as “private” by a programmer. It’s kind of a convention so that the next person (or yourself) using your code knows that a name starting with _ is for internal use.

As the Python documentation says:

a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

Otherwise, a single leading underscore isn't exactly just a convention: if you use from <module/package> import *, none of the names that start with an _ will be imported unless the module’s/package’s __all__ list explicitly contains them.

Ref:
Underscores in Python
What is the meaning of a single- and a double-underscore before an object name?