1

I have seen underscore character used in python variables. Does the interpreter interpret variables differently with presence of underscore _ or is it purely a matter of convention? If it's convention, is there a standard document on the naming convention in python variables?

I'm using python 3.8.5

3
  • 1
    As per my knowledge, it is nothing special. It is special in REPL where it is used as a variable where the last valid output gets stored. Commented Aug 11, 2020 at 5:11
  • Read all occurrences of "underscore" in PEP 8. Commented Aug 11, 2020 at 5:20
  • 1
    One underscore at the beginning does not matter, two underscores do. Commented Aug 11, 2020 at 5:22

2 Answers 2

7
  1. Single Leading Underscore: _var The underscore prefix is meant as a hint to another programmer that a variable or method starting with a single underscore is intended for internal use. This convention is defined in PEP 8.

  2. Single Trailing Underscore: var_ Sometimes the most fitting name for a variable is already taken by a keyword. Therefore names like class or def cannot be used as variable names in Python. In this case you can append a single underscore to break the naming conflict.

  3. Double Leading Underscore: __var With Python class attributes (variables and methods) that start with double underscores, things are a little different. A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

  4. Double Leading and Trailing Underscore: var Perhaps surprisingly, name mangling is not applied if a name starts and ends with double underscores. Variables surrounded by a double underscore prefix and postfix are left unscathed by the Python interpeter.

These paragraphs are taken from https://dbader.org/. Please check the page for more detailed information and examples.

Sign up to request clarification or add additional context in comments.

Comments

3

By convention, a variable beginning with a single underscore as in _some_var is considered private - meaning that external entities should not expect its use to remain stable across releases or ever be in any generally sane state. Its slightly more than just convention. from somemodule import * will not add these "private" variables to the module namespace.

From PEP8, the standard style guide for python:

[T]he following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose names start with an underscore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.