Loading video player…

Single and Double Underscore Naming Conventions in Python (Overview)

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 video course, 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 classes
  • Explore other common uses of underscores in Python names
Download

Course Slides (.pdf)

7.4 MB
Download

Sample Code (.zip)

4.7 KB

00:00 Welcome to this course where you will be learning everything there is to know about single and double underscores in Python names. If you have been exploring libraries or you have been reading someone else’s code, you might have come across variables or other objects that start with an underscore and some that don’t.

00:19 And some start with two underscores or even end with an underscore, or start with two and end with two. There’s even the use of the underscore on its own.

00:28 What’s all that about? Well, that’s what this course will explain in quite a bit of detail.

00:34 In this course, you will learn how to follow naming conventions in Python for better readability because you will find that these underscores and the way they are being used is all linked with naming conventions.

00:48 You will learn how to differentiate between public and non-public Python attributes. You will learn how to signal the use of non-public attributes explicitly, for example, with _validate so you start with an underscore.

01:02 You will learn how to enable name mangling in multiple inheritance scenarios. For example, with double underscore, so __attribute. You will learn how to define special methods, also known as dunder methods or magic methods.

01:17 For example, the __len__ method. And you will learn how to avoid a clash with Python’s reserved keywords and built-ins, for example, list.

01:28 So the contents of this course is as follows. We’ll start with naming conventions, talking about __ __ and PEP 20, public and non-public interfaces, but leading underscores for non-public objects, name mangling in multiple inheritance.

01:43 We’ll talk about Python’s special methods, dunder methods or magic methods. You learn about conflicts with Python keywords and built-in names. And finally, you’ll learn about additional uses of the underscore in Python.

01:57 This course touches on a few topics that ideally you will have some level of familiarity with. You’ll be studying examples that involve constants and functions and variables and classes and methods and attributes.

02:11 And if these topics are completely new to you, that’s no problem. There are a number of excellent tutorials to get you up to speed, and I’ll include links to these tutorials in the course.

02:22 I am really looking forward to going on this journey with you. So let’s get started.

Become a Member to join the conversation.