Following are different places where _ is used in Python:
- Single Underscore:
- In Interpreter
- After a name
- Before a name
- Double Underscore:
- __leading_double_underscore
- __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 20>>> _ 20>>> _ * 240>>> _ 40>>> _ / 220 |
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
Example:
>>> class MyClass(): ... def __init__(self): ... print ("OWK") >>> def my_defination(var1 = 1, class_ = MyClass): ... print (var1) ... print (class_) >>> my_defination() 1__main__.MyClass >>> |
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 10>>> test._private 12 |
Python class_file.py
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
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.
testFile.py
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, inAttributeError: Myclass instance has no attribute '__variable'nce has no attribute 'Myclass'>>> obj._Myclass__variable 10 |
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() 10 |
__BEFORE_AFTER__
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) 2>>> obj.__add__(5,2) 10 |
This article is contributed by Nirmi Shah. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Recommended Posts:
- Python Lambda with underscore as an argument
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
Improved By : vishalsingh80

