- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Private Methods in Python
In Python, we can define the private method by prefixing the single underscore to the method name and like the other programming languages we cannot prevent accessing the private method outside the class in python. Simply we can say the private method is used only internally for the defined class.
What is private method and public method?
A private method is the method that should be called only inside the defined class whereas the Public method is defined within the class, can be called on an instance of the defined class.
Now let’s define the private method and see how the private method is differed from the public method.
Example
In this example we are going to create a base class by defining the public method and private methods inside the class and after that we will check whether the private method can be accessible by the other class or outside the class.
# Base class class Base: # Defining the public method def pub(self): print("This is Public method") # Defining the private method def __priv(self): print("This is Private method") # Now we are creating the derived class for calling the priivate and public methods created in the base class. class Derived(Base): def __init__(self): Base.__init__(self) def call_pub(self): # Calling public method from base class print("\nInside derived class") self.pub() def call_priv(self): # Calling private method of base class self.__priv() out = Base() # Calling public method out.pub() out2 = Derived() out2.call_pub()
Output
As we know that public class can be called even outside the class, so in the output it returns the data that is available in the public method.
This is Public method Inside derived class This is Public method
Example
In the above example we called the public method using the derived class now in the same way we are going to call the private method of the base class using the derived class then the attribute error will be raised as the private method cannot be used outside the class or cannot be called through the other class.
# Base class class Base: # Defining the public method def pub(self): print("This is Public method") # Defining the private method def __priv(self): print("This is Private method") # Now we are creating the derived class for calling the priivate and public methods created in the base class. class Derived(Base): def __init__(self): Base.__init__(self) def call_pub(self): # Calling public method from base class print("\nInside derived class") self.pub() def call_priv(self): # Calling private method of base class self.__priv() out = Base() out.__priv() out2 = Derived() out2.call_priv()
Output
As we called the private method of the base class using the Derived class the attribute error will be raised.
Traceback (most recent call last): File "/home/cg/root/16002/main.py", line 22, in <module> out.__priv() AttributeError: 'Base' object has no attribute '__priv'
Example
To call the private method outside the class we have a way that is by using the public method. In this example we will create the private and public methods with the class along with another public method to call the private method.
# Creating a class class private_class: # creating the public method def pub(self): print("This is Public method") # creating the private method def __priv(self): print("This is Private method") # Calling private method by using the another public method def calling_priv(self): self.pub() self.__priv() out = private_class() out.calling_priv()
Output
This is Public method This is Private method