What's the meaning of underscores (_ & __) in Python variable names?

Published: 18 April 2017
on channel: Real Python
425,283
11k

https://dbader.org/python-tricks ► Make those underscores count and write clean and beautiful Python, by example

Leading underscores in Python variable names (for example _foo and __foo) protect developers from naming conflicts. In this video you'll learn how to work with these Pythonic coding conventions and why they exist.

A single underscore in front of a variable name (prefix) is a hint that a variable is meant for internal use only.

A double underscore prefix causes the Python interpreter to rewrite the variable name in order to avoid naming conflicts in subclasses. Double underscores are also called "dunders" in Python.

Watch the video tutorial to see the details of how double underscore name mangling works and how it affects your own Python classes.

FREE COURSE – "5 Thoughts on Mastering Python" https://dbader.org/python-mastery

SUBSCRIBE TO THIS CHANNEL: https://dbader.org/youtube

* *

► Python MUGS, T-SHIRTS & MORE: https://nerdlettering.com

FREE Python Tutorials & News:
» Python Tutorials: https://dbader.org
» Python News on Twitter:   / @dbader_org  
» Weekly Tips for Pythonistas: https://dbader.org/newsletter
» Subscribe to this channel: https://dbader.org/youtube