Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python private method

# Python program to 
# demonstrate private methods
  
# Creating a Base class 
class Base: 
  
    # Declaring public method
    def fun(self):
        print("Public method")
  
    # Declaring private method
    def __fun(self):
        print("Private method")
  
# Creating a derived class 
class Derived(Base): 
    def __init__(self): 
          
        # Calling constructor of 
        # Base class 
        Base.__init__(self) 
          
    def call_public(self):
          
        # Calling public method of base class
        print("
Inside derived class")
        self.fun()
          
    def call_private(self):
          
        # Calling private method of base class
        self.__fun()
  
# Driver code 
obj1 = Base()
  
# Calling public method
obj1.fun()
  
obj2 = Derived()
obj2.call_public()
  
# Uncommenting obj1.__fun() will 
# raise an AttributeError 
  
# Uncommenting obj2.call_private() 
# will also raise an AttributeError
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #python #private #method
ADD COMMENT
Topic
Name
3+2 =