Single and Double Underscores in Python Names
Python has a few important naming conventions that are based on using either a single or double underscore character (
_). These conventions allow you to differentiate between public and non-public names in APIs, write safe classes for subclassing purposes, avoid name clashes, and more.
Following and respecting these conventions allows you to write code that looks Pythonic and consistent in the eyes of other Python developers. This skill is especially useful when you’re writing code that’s intended for other developers to work with.
In this tutorial, you’ll:
- Learn about Python naming conventions that rely on using underscores (
- Differentiate public and non-public names by using a single leading underscore
- Use double leading underscores to leverage name mangling in Python