Underscores (_) hold special significance in python language. They are used in different places in python.
You might have seen underscores used in python like this before,
- for _ in range(5):
- _=5
- __init__(self)
There are a lot more cases in which underscores are used in different ways in python.
Single Underscore(_):
1- Use in Interpreter:
2- Ignoring the values:
We can also ignore multiple values by using (*_)
. It is also known as External Unpacking.
This is also known as Tuple Unpacking.
3- Use in looping:
Underscore can be used as the variable in the loop.
for _ in range(5):
print(_)
Output:
0
1
2
3
4
4- Separating digits of numbers:
a=1_000_000
print(a)
Output:
1000000
It can also be used to separate binary, octal, hexadecimal numbers.
print(0b_1000) #Binary
print(0x_21_10) #Hexa-decimal
print(0o_25) #Octal
Output:
8
8464
21
We can also use the single underscore as the naming of variables, class and functions.
Single Leading Underscore(_variable):
class A:
def __init__(self):
self.name = "CodesDope"
self._id=101
obj = A()
print(obj.name)
print(obj._id)
Output:
CodesDope
101
While importing, from A import * does not import objects whose names start with an underscore.
#filename is main.py
#variable name having single leading underscore.
_a="helloworld"
def mul(x,y):
print (x*y)
#function name having single leading underscore
def _sub(x,y):
print (x-y)
Importing main.py to test.py
from main import *
#accessing variable names having single leading underscore.
print (_a) #Output:NameError: name '_a' is not defined
#accessing function name having single leading underscore
print (_sub(7,2)) #Output:NameError: name '_sub' is not defined
#accessing variables and functions which is not having single leading underscore, we will able to access those variables and functions
mul(3,4) #Output:12
If we want to import variables and functions having a single leading underscore, we have to mention the name while importing.
from main import _a,_sub
#accessing variable names having single leading underscore.
print (_a)
#accessing function name having single leading underscore
_sub(7,2)
Output:
helloworld
5
Single trailing underscore(variable_):
list=[1,2,3]
t=(5,6,7)
#Coverting tuple to list using list() constructor
t1=list(t)
Output:
TypeError: 'list' object is not callable
We can resolve it by using list_
list_=[1,2,3]
t=(5,6,7)
#Coverting tuple to list using list() constructor
t1=list(t)
print (t1)
Output:
[5, 6, 7]
Double Pre Underscore (__variable):
Name Mangling: interpreter of the Python alters the variable name in a way that it is challenging to clash when the class is inherited.
self._className__methodname() instead of self.__methodname()
class A:
def __init__(self,name,id):
self.name=name
self.__id=id
a=A("CodesDope",101)
print (a.name)
print (a.__id
Output:
CodesDope
print (a.__id)
AttributeError: 'A' object has no attribute '__id'
To resolve this problem we have to write:
class A:
def __init__(self,name,id):
self.name=name
self.__id=id
a=A("CodesDope",101)
print (a.name)
print (a._A__id) //Name Mangling
Output:
CodesDope
101
Double leading and trailing underscore (__variable__):
class A():
def __init__(self):
self.__num__ = 101
a= A()
a.__num__
Output:
101
Yeah we did it!
Now you can move to next chapter.
Learn the Python Programming Language and strengthen your grip on the subject.