Python Underscores Under Estimated

Sumit Chaudhary
weCode
Published in
4 min readNov 17, 2020
Photo from Unsplash

In python while programming many times you may have came across underscore things.

There are many different ways python underscore like :-

  1. _var (Single leading underscore)
  2. var_ (Single trailing underscore)
  3. __var (Double leading underscore)
  4. var__ (Double trailing underscore)
  5. __init__ (Dunder — Double Underscore)
  6. “_” (Single underscore)

Single Leading Underscore : “_var”

The underscore prefix is meant as a hint to tell another programmer that any variable/method that starts with a single leading underscore is intended for private use only and should not be exposed to outside world.

This is up-to the programmer to use this kind of convention for private variables/methods this is not enforced by python interpreter. And if someone knows the variable they can still access it since python don’t have any public/private distinctions.

But single leading underscore has visible effect on import statements. Lets take an example:

Here we can see that if you use wildcard import to import all the names from the module, Python will not import names with a leading underscore ( unless the module defines an __all__ list that overrides this behavior)

Wildcard import error

As a short note, wildcard imports should be avoided (PEP 8 recommendation) as they make it unclear which names are present in the namespace. Unlike wildcard imports regular imports are not affected by the leading single underscore naming convention.

To fix above error just uncomment Line 3 in importer.py

Single Trailing Underscore : “var_”

A single trailing (postfix) underscore is used by convention to avoid naming conflicts with Python keywords. For example you may want to use class keyword but that’s reserved so what you can do is append “_” at the end like class_.

Double Leading Underscore : “__var”

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses.

Sounds confusing ? Let’s take a look through examples

When you run this file you can see that all variables except __baz are present as it is. __baz has been modified by python and this is known as name mangling — the interpreter changes the name of the variable in a way that makes it harder to create collisions when this subclass is extended later.

To access the variable from outside (using instance) you need to follow the convention : _CLASSNAME__var and what about overriding in subclass ? Actually you can’t override as I said, you can define one more variable in subclass with name and access both of them using the above convention as shown below:

As you can see Line 22 throws error so the correct way to access double leading variables is to use the way shown in Line 21.

Name mangling applies to methods as well as shown in below:

Mangled Method

You can also use mangled variable defined in global space via Class Instance:

Double Leading And Trailing Underscore : “__var__”

Names that have both leading and trailing underscores are reserved for special use in the language like __init__, __call__ , __enter__ , __exit__ etc.

Single Underscore : “_”

You might have seen this lots of times in python code. This is used for “don’t care” variables to ignore their values.

fruits = ('red', 'mango', '10', 'kg')
color, fruit, _, unit = fruits
>>> color
red
>>> _
10

Besides its use as temporary variable , “_” is a special variable in most Python REPLs that represents the result of the last expression evaluated by the interpreter.

Single Underscore as Temporary holder
Single Underscore as Temporary holder

--

--

Sumit Chaudhary
weCode
Editor for

Software Engineer, Java/Python, Machine Learning