Skip to content
Related Articles

Related Articles

Underscore (_) in Python
  • Difficulty Level : Medium
  • Last Updated : 22 Nov, 2020

Following are different places where _ is used in Python:

  1. Single Underscore:
    • In Interpreter
    • After a name
    • Before a name
  2. Double Underscore:
    1. __leading_double_underscore
    2. __before_after__

Single Underscore

In Interpreter:
_ returns the value of last executed expression value in Python Prompt/Interpreter

>>> a = 10
>>> b = 10
>>> _
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> a+b
>>> _
>>> _ * 2
>>> _
>>> _ / 2

For ignoring values:
Multiple time we do not want return values at that time assign those values to Underscore. It used as throwaway variable.

# Ignore a value of specific location/index
for _ in range(10)
    print ("Test")
# Ignore a value when unpacking
a,b,_,_ = my_method(var1)

After a name
Python has their by default keywords which we can not use as the variable name. To avoid such conflict between python keyword and variable we use underscore after name


>>> class MyClass():
...     def __init__(self):
...             print ("OWK")
>>> def my_defination(var1 = 1, class_ = MyClass):
...     print (var1)
...     print (class_)
>>> my_defination()

Before a name
Leading Underscore before variable/function/method name indicates to programmer that It is for internal use only, that can be modified whenever class want.

Here name prefix by underscore is treated as non-public. If specify from Import * all the name starts with _ will not import. Python does not specify truly private so this ones can be call directly from other modules if it is specified in __all__, We also call it weak Private

class Prefix:
...     def __init__(self):
...             self.public = 10
...             self._private = 12
>>> test = Prefix()
>>> test.public
>>> test._private


def public_api():
    print ("public api")
def _private_api():
    print ("private api")

Calling file from Prompt

>>> from class_file import *
>>> public_api()
public api
>>> _private_api()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_private_api' is not defined
>>> import class_file
>>> class_file.public_api()
public api
>>> class_file._private_api()
private api

Double Underscore(__)

Leading double underscore tell python interpreter to rewrite name in order to avoid conflict in subclass.Interpreter changes variable name with class extension and that feature known as the Mangling.

class Myclass():
    def __init__(self):
        self.__variable = 10

Calling from Interpreter

>>> import testFile
>>> obj = testFile.Myclass()
>>> obj.__variable
Traceback (most recent call last):
File "", line 1, in
AttributeError: Myclass instance has no attribute '__variable'
nce has no attribute 'Myclass'
>>> obj._Myclass__variable

In Mangling python interpreter modify variable name with ___. So Multiple time It use as the Private member because another class can not access that variable directly. Main purpose for __ is to use variable/method in class only If you want to use it outside of the class you can make public api

class Myclass():
    def __init__(self):
        self.__variable = 10
    def func(self)
        print (self.__variable)

Calling from Interpreter

>>> import testFile
>>> obj = testFile.Myclass()
>>> obj.func()

Name with start with __ and ends with same considers special methods in Python. Python provide this methods to use it as the operator overloading depending on the user.

Python provides this convention to differentiate between the user defined function with the module’s function

class Myclass():
    def __add__(self,a,b):
        print (a*b)

Calling from Interpreter

>>> import testFile
>>> obj = testFile.Myclass()
>>> obj.__add__(1,2)
>>> obj.__add__(5,2)

This article is contributed by Nirmi Shah. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :