[Python Basics] underlined variables

Yean owned cloud around teeth Tuo Jiang Han Star video heron sleep sand.
Sudeten pedestrian Chang Wang Liu, who worked with the king sweep falling.

I am usually very common to the python variable is underlined in two ways:

  1. Front and rear double underline, understand my previous meta data information is similar to the python program, such as __name__variable
  2. Before the single underline, python class private variables or function
  3. Single underline, used to represent the output (in the python Interactive console). Common usagefor _ in theList

Today encountered some new challenges:

  • `from feature import absolute_import
  • author = "JOJO"

In the bright future "to leave a record in order to strengthen memory", I retrieve and select the site interpretation translation (once I somehow do not like the translation, but now they have quite good enough. Alas)

There are five variables underlined Python

  • Front single underline
  • Rear single underline
  • Front double underline
  • Front and rear double underscore
  • Single underline

Front single underline

In importtime, the front end part to the object will not be introduced at the beginning of underscore (internal function)

# This is my_module.py:

def external_func():
    return 23

def _internal_func():
    return 42
>>> from my_module import *
>>> external_func()
23
>>> _internal_func()
NameError: "name '_internal_func' is not defined"

But as a conventional variable introduced (regular imports) can still be read:

>>> import my_module
>>> my_module.external_func()
23
>>> my_module._internal_func()
42

Rear single underline

In general, the post and the single underline is used to avoid conflicts python built Image

>>> def make_object(name, class):
SyntaxError: "invalid syntax"

>>> def make_object(name, class_):
...     pass

Front double underline

Front double underscore name is called tear? (Name mangling). python variables interpreter will rename the face of such variables, thereby forming effect (internal parameters) one can access only the internal method:

class Test:
    def __init__(self):
        self.foo = 11
        self._bar = 23
        self.__baz = 23
>>> t = Test()
>>> dir(t)
['_Test__baz', '__class__', '__delattr__', '__dict__', '__dir__',
 '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__',
 '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
 '__weakref__', '_bar', 'foo']
class ManglingTest:
    def __init__(self):
        self.__mangled = 'hello'

    def get_mangled(self):
        return self.__mangled

>>> ManglingTest().get_mangled()
'hello'
>>> ManglingTest().__mangled
AttributeError: "'ManglingTest' object has no attribute '__mangled'"

Front and rear double underscore

This variable is reserved python interpreter, naming and access are normal.

Single underline

Temporary variables

Next you need work done:
PEP8 programming specification

Guess you like

Origin www.cnblogs.com/ziangzhang/p/11964844.html