I recently received a question from someone in the Social Techies Discord channel, our tech community, about why some of my variables have “_” in front of them. This is not the first time I’ve been asked this, and when I started Python, I was curious myself. Some variables have underscores in front of them, some have underscores behind them. There are even variables with double underscore prefixes, and functions surrounded by double underscores. In this post, we’re going to take a look at:
- Different ways to use Underscores in Python variable names
- Underscore Prefix or Suffix Variables in Python
- Double Underscore (“Dunder”) Prefix Variables in Python
- Functions or Variables Surrounded by a Double Underscore
- Using the Underscore as a Variable Name in Python
- Summary of Underscores in Python Variables
Different ways to use Underscores in Python Variable Names
The basic usage of underscores in Python variable names is to separate words. However, there are four “special” ways to use underscores in variable names as well. First we’ll cover using a single underscore prefix or suffix. Second, we’ll cover using a double underscore prefix in variable names. Third, we’ll cover function and variable names surrounded by a double underscore in Python. Last, we’ll cover the usage of the underscore itself as a variable name.
_ or Underscore Prefix or Suffix Variables in Python
Conventionally, underscores are used to augment reserved keywords in Python to use as variable names. Many libraries attach an underscore suffix to some keywords like
class to make it
class_. For example, the native Python
gettext library uses
class_ in its translation function. You can also use it in front of a variable name like I do with
_dict in this example.
The other usage of an underscore prefix or suffix is for “semi private” variables. The single underscore does not enforce the functionality of private variables, but rather acts as a warning to an editor. If you are playing around with a code library and you see a function with either a single underscore prefix or suffix, don’t touch it.
__ or Double Underscore (“Dunder”) Prefix Variables in Python
Double underscore prefixes define private variables in Python. Sometimes this is also referred to as a “dunder”. Variables with these names have their local scope enforced. When a variable in a function is declared with a double underscore, Python mangles that variable’s name so it cannot be used outside of the scope. Let’s see an example of this below.
We’ll create a sample class called
Shape like we did in the introduction to classes tutorial. In this class, we’ll create a
__name property that can be set with a function. We’ll also create a way to access the
__name property inside the function.
class Shape: def __init__(self, sides): self.sides = sides def name(self, name): self.__name = name def get_name(self): return self.__name triangle = Shape(3) triangle.name("t1") print(triangle.get_name()) print(triangle.sides) print(triangle.__name)
The code above assigns a name to the triangle
Shape and prints out the name when we access it from inside the class. Notice from the image below that although we can access
sides directly, we cannot directly access the name.
Functions or Variables Surrounded by __ (Double Underscore)
A third use of underscores in variable names in Python are function names surrounded by double underscores. Examples of this include
__main__. These are special functions and properties in Python that the interpreter looks for when running code.
For example, in the code in the section above, we have an
__init__ function. The interpreter looks for this function when we create the
Shape variable. Every class has to have an
__init__ function. If you don’t define a custom one, your class will use the default object
__init__ function. This is equivalent to a “constructor” function in Java.
Another thing you’ll see quite often in Python functions that use these double underscores is the statement
if __name__ == “__main__”. The
__name__ variable/property is a built-in Python variable that stores the current “name” of the executing function. In this case
__main__ is the value that the built-in
__name__ variable is assigned when we run a Python script.
_ or Underscore as a Variable Name in Python
Finally we come to the last special usage of underscores in Python variable names, using the underscore itself as a variable name. This is done when we don’t need a variable. For example, in Java or C++, many for loops may be set up in the format
for i … and never use the
i variable in the loop. In Python, the conventional way to do that is to use an underscore instead. For example,
for _ in range(12) will loop 12 times.
Another reason to use an underscore as a variable name is if you don’t need a returned variable. For example, let’s say you’re calling a function that returns two variables, but you only need the second one. Then, you would structure your call like
_, variable = function().
Summary of Underscores in Python Variable Names
In this post, we covered uses of the underscore in Python variable names. We learned about the basic usage of underscores in separating words in variable names as well as four special cases. The first special case we learned about is the underscore prefix or suffix to modify reserved names and use them as variables. Using the underscore prefix or suffix marks variables as “private” to any editors of the code.
The second case we looked at is the double underscore (dunder) prefix. This setup marks the variable as private to the interpreter, which enforces the “privateness” of the variable. The third case we looked at is when a variable or function is surrounded by double underscores. These variables or functions have special uses by the interpreter. Finally, we looked at the usage of the underscore as a variable name itself. This use case is for temporary variables that we can throw away or use simply as a counter.
- Download a YouTube Transcript in 3 Lines of Python
- Run Multiple Functions in Parallel in Python 3
- Build an AI Content Moderation System
- Dijkstra’s Algorithm in Python
- Build Your Own AI Text Summarizer in Python
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.DonateDonate monthlyDonate yearly
2 thoughts on “_ and __ Variables in Python”