python Basics_ Underline

1. Single leading underline

  • A single underscore is a python naming convention that indicates that the name is for internal use. It is not usually enforced by the Python interpreter, just as a hint to the programmer.

  • If you use wildcards to import all names from a module (from unittest import *), Python will not import names with leading underscores (unless the module defines a _all _listthat overrides this behavior)

  • Regular import (eg: from unittest import _fun) is not affected by the naming convention of leading single underscores.

2. Single end underline

  • A single end underscore (suffix) is a convention to avoid naming conflicts with Python keywords.

3. Double leading underline

  • Double underscore prefixes cause the Python interpreter to override attribute names to avoid naming conflicts in subclasses.
class Test:
   def __init__(self): = 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']

Explanation:__baz Rewritten by interpreter as'_Test__baz'. This is done to prevent variables from being overridden in subclasses

# Create another class that extends the Test class and try to override the existing properties added in the constructor
class ExtendedTest(Test):
   def __init__(self):
       super().__init__() = 'overridden'
       self._bar = 'overridden'
       self.__baz = 'overridden'

>>> t2 = ExtendedTest()
>>> t2._bar
>>> t2.__baz
AttributeError: "'ExtendedTest' object has no attribute '__baz'"

>>> dir(t2)
['_ExtendedTest__baz', '_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', 'get_vars']

>>> t2._ExtendedTest__baz

Explanation:__baz become_ExtendedTest__baz To prevent accidental modification;

4. Double leading and double ending underline

  • python comes with special functions, such as

    __init__Object constructor, or__call__ --- It enables an object to be called.
  • It is best to avoid using names that begin and end with double underscores ("dunders") in your own programs to avoid conflicts with future changes in the Python language.

5. Single underline

  • "_" can be used as a placeholder variable (temporary variable);

  • Represents the result of the most recent expression evaluated by the interpreter.

    >>> 20 + 3
    >>> _
    >>> print(_)
    >>> list()
    >>> _.append(1)
    >>> _.append(2)
    >>> _.append(3)
    >>> _
    [1, 2, 3]

6. Summary

patterngive an examplemeaning
Single leading underline_varNaming convention, for internal use only. Usually, it is not enforced by the python interpreter (except wildcard import), which is only used as a prompt for programmers.
Single end underscorevar_Use as agreed to avoid naming conflicts with python keywords, such as class_
Double leading underline__varName decoration is triggered when used in the context of a class. Enforced by the python interpreter. As defined in class test__ The variable bar will eventually be interpreted as_ Test__bar
Double leading and double trailing underscores__var__Represents a special method defined by the python language. Avoid using this naming in your own properties
Separate underline_Sometimes used as a name for temporary or meaningless variables ("don't care"). It also represents the result of the latest expression in Python repl.

7. Reference links

Keywords: Python

Added by sirfartalot on Fri, 10 Sep 2021 00:49:54 +0300