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):
There are a lot more cases in which underscores are used in different ways in python.
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(_)
4- Separating digits of numbers:
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
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)
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)
Single trailing underscore(variable_):
list=[1,2,3] t=(5,6,7) #Coverting tuple to list using list() constructor t1=list(t)
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)
[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
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
Double leading and trailing underscore (__variable__):
class A(): def __init__(self): self.__num__ = 101 a= A() a.__num__
Yeah we did it!
Now you can move to next chapter.
Learn the Python Programming Language and strengthen your grip on the subject.