Wednesday, November 21, 2018

Private variable and method in Python

Python doesn't have real private variables and methods. Thus there is an adopted convention of having two underlines at the beginning make a variable and a method private. The following example explains this behavior:

class Amount():
   
    __value = 50
   
    def __display(self):
       
        print ("In class A")
  
 def myvalue(self):
       
        print("Value is "+ str(Amount.__value))   
       

obj1 = Amount()
obj1.myvalue()
obj1.__display()
print(obj1.__value)

In the preceding program, __display() is the private method and __value is the private variable. Let's see the output:



In the myvalue() method, the __value variable can be accessed as shown in the output (Value is printed). Thus outside the class, we cannot access the private method as well as the private variable, but inside the class, we can access the private variables.

Now, let's rewrite the program and try to access private variables and the private method from outside the class.

class Amount():
   
    __value = 50
   
    def __display(self):
       
        print ("In class A")
    def myvalue(self):
       
        print("Value is "+ str(Amount.__value))   
       

obj1 = Amount()
obj1.myvalue()
obj1._Amount__display()
print(obj1._Amount__value)


The output of the program is shown below:



Thus we can access private variables and the private method from outside the class using the syntax like instance _class-name__private-attribute.(Example obj1._Amount__value , obj1._Amount__display())

The benefit of accessing private variable outside the class is to prevent the class method and variable being changed by others accidentally. See the example below:

 class Amount():
  
    __value = 50
  
    def __display(self):
      
        print ("In class Amount")
      
    def display(self):
      
        self.__display()  
      
      
class new(Amount):
  
    def __display(self):
      
        print ("In class new")
  

obj1 = Amount()
obj1.display()


obj2 = new()
obj2.display()





As you can see, obj2.display() didn't call obj2.__display() as we could expect. Actually this is the correct behavior for __. So when you create a method starting with __ you're saying that you don't want anybody to override it, it will be accessible just from inside the own class.

In Python, there is a concept called name mangling, which means that there is a limited support for a valid use-case for class-private members basically to avoid name clashes of names with names defined by subclasses. Any identifier of the form __display(at least two leading underscores or at most one trailing underscore) is replaced with _classname__display, where classname is the current class name with leading underscore(s) stripped. As long as it occurs within the definition of the class, this mangling is done. This is helpful for letting subclasses override methods without breaking intraclass method calls.


Private variables and methods are used for special purpose hence during coding use of _ and __ should be avoided unless you wanna specify private behavior. Here we end our discussion. In the next post we'll discuss about file handling in Python.Till then keep learning Python as Python is easy to learn!  
Share:

0 comments:

Post a Comment