Encapsulation in Python

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those type of variables are known as private varibale.

A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.



Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”. As using encapsulation also hides the data. In this example, the data of any of the sections like sales, finance or accounts are hidden from any other section.

Protected members

Protected members (in C++ and JAVA) are those members of the class which cannot be accessed outside the class but can be accessed from within the class and it’s subclasses. To accomplish this in Python, just follow the convention by prefixing the name of the member by a single underscore “_”.

Note: The __init__ method is a constructor and runs as soon as an object of a class is instantiated.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# demonstrate protected members
  
  
# Creating a base class
class Base:
    def __init__(self):
          
        # Protected member
        self._a = 2
  
# Creating a derived class    
class Derived(Base):
    def __init__(self):
          
        # Calling constructor of
        # Base class
        Base.__init__(self
        print("Calling protected member of base class: ")
        print(self._a)
          
obj1 = Base()
  
# Calling protected member
# Outside class will  result in 
# AttributeError
print(obj1.a)
  
obj2 = Derived()

chevron_right


Output:

Calling protected member of base class: 
2
Traceback (most recent call last):
  File "/home/6fb1b95dfba0e198298f9dd02469eb4a.py", line 25, in 
    print(obj1.a)
AttributeError: 'Base' object has no attribute 'a'

Private members

Private members are similar to protected members, the difference is that the class members declared private should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private instance variables that cannot be accessed except inside a class. However, to define a private member prefix the member name with double underscore “__”.

Note: Python’s private and protect member can be accessed outside the class through python name mangling.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to 
# demonstrate private members
  
# Creating a Base class
class Base:
    def __init__(self):
        self.a = "GeeksforGeeks"
        self.__c = "GeeksforGeeks"
  
# Creating a derived class
class Derived(Base):
    def __init__(self):
          
        # Calling constructor of
        # Base class
        Base.__init__(self
        print("Calling private member of base class: ")
        print(self.__a)
# Driver code
obj1 = Base()
print(obj1.a)
  
# Uncommenting print(obj1.c) will
# raise an AttributeError
  
# Uncommenting obj2 = Derived() will
# also raise an AtrributeError as
# private member of base class 
# is called inside derived class

chevron_right


Output:

GeeksforGeeks
Traceback (most recent call last):
  File "/home/ee6c98f658e288878c9c206332568d9a.py", line 24, in 
    print(obj.__c)
AttributeError: 'Test' object has no attribute '__c'

Traceback (most recent call last):
  File "/home/abbc7bf34551e0ebfc889c55f079dbc7.py", line 26, in 
    obj2 = Derived()
  File "/home/abbc7bf34551e0ebfc889c55f079dbc7.py", line 18, in __init__
    print(self.__c)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.