Name Mangling, Operator Overloading, Local and Global Variables
In languages like C++ and Java, we have access specifiers like private, but in python we don't have any such concept. We can use concept of hidden variable
We can create attributes using __variable_name and same can be called using _ClassName__attibute
#Name Mangling
class Test:
__x = 20 #hidden Variable
def __init__(self):
self.__h = 10
obj = Test()
print(obj._Test__h)#call using _ClassName__variable
print(Test._Test__x) #call using _ClassName__variable
If we don't want the variable to be accessed directly; using data mangling this would be treated as special members.
It can be accessed inside class like normal variables but out class should be accessed using _className__variable
Operator Overloading
class int:
def __add(self,other):
pass
x = 5 +2 # x = int.__add__(self,other)
#Here, 5 and 2 are two objects (self,other)
# And int class invokes default add method and pass them
#as argument.
Similar to add method, there are separate method to perform elementary operations such as subtraction,multiplication,power,negative,unary operations etc.
''''
+ -> __add__()
- -> __sub__()
* -> __mul__()
// -> __floardiv()
/ -> __truediv()
== -> __eq__()
- -> __neg__()
% -> __mod__()
lt -> __lt__()
gt -> __gt__()
ge -> __ge__()
'''
Example - Create a class to add length of two objects of type Line.
class Line:
def __init__(self,length):
self.length = length
def show(self):
print("Length = ",self.length)
def __add__(test,other):#add two length of objects provided
return Line ((test.length + other.length))
# return value as new object
l1 = Line(5) # 5 length of object of type Line
l2 = Line(8) # 8 is length of other object of type Line
l3 = l1+l2 # Line class calls its add method and
#passes length two objects
l3.show()
Note: By default in int class we have add function. But here we need to define add function which takes two argument (objects) and return a new object which is sum of length of two objects.
'''
Types of variables
i) Global variable
ii) Local variable
iii) Class variable (static variable)
iv) Instance Variable
'''
#Example 1
def f1():
x = 10 # local variable
print(x)
f1()
print(x) # NameError: name 'x' is not defined
'''Scope of variable is within the function
Once function call is completed, variable gets destroyed
Hence, local variable have scope within function'''
'''Global Variable'''
x = 20
def f1():
print(x)
print(x) # 20 is printed as x is global variable
# Global variables are defined outside class/function
# Scope is global and lifetime is till program end
'''Local vs Global'''
x = 20
def f1():
x = 10 #local variable
print(x) # preference is given to local variable even
# same variable exist outside block
f1() # prints 10
print(x) # local variable x gets destroyed once f1() is completed
# hence, uses global variable and 20 gets printed
Here, x has two scope one global and one local. Inside function by assigning a variable x different value; Infact we are creating new local variable.
If we want to treat x as global then it should be done as below.
x = 20
def f1():
global x # updates global variable to 10
x = 10
print(x)
f1()
print(x) #output is 10 not 20 because x is updated
globals()
Python provides global() function which provides list of global variables.
x = 20 # global variable
def f1():
globals()['x'] = 10 #modifying global variables
x = 5 #local variable
print(globals()['x']) #print global
print(x)# print local
f1()
print(x) #print global
output
10
5
10
Comments
Post a Comment