Welcome

sampleqa.in tutorials, Python Programming  tutorial,Python Classes

Python Classes:       Private Variables and Private Methods


By Default all of object's variables and methods are public, meaning that they can be directly accessed or invoked by a client. To enocurage encapsulation , a method or a variable can be private, so that only methods of that class can access these private methods/variables. That means client cannot directly invoke these private methods or methods.

Private Variables

      	class One:
      	  def __init__(self , name,age)
      	    self.name = name
      	    self.__age = age
      	  
      	  def print(self):
      	    print("Name is {} Age is {}".format(self.name,self.__age) )
      	
	

Private Methods

class One:
  def __init__(self , name,age):
    self.name = name
    self.__age = age
  def __printf(self):
    print("Name is {} Age is {}".format(self.name,self.__age) )	
  def printf(self):
    self.__printf()


o=One("sam",44)
o.printf()


--------------Output------------------
Name is sam Age is 44