0% found this document useful (0 votes)
10 views

Encapsulation in Python

Uploaded by

Bandari Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Encapsulation in Python

Uploaded by

Bandari Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Encapsulation in Python is one of the key concepts of Object-Oriented Programming (OOP).

It
refers to the bundling of data (variables) and methods (functions) that work on the data within one
unit or class, restricting access to some of the object's components. Here are the key concepts
covered under Encapsulation in Python:

1. Private Variables and Methods


Private Members: These are variables and methods that should not be accessed directly from
outside the class. In Python, private members are denoted by prefixing an identifier with two
underscores ( __ ).

Example:
python Copy code

class Example: def __init__(self): self.__private_var = "I am private" def


__private_method(self): print("This is a private method")

2. Protected Variables and Methods


Protected Members: These are variables and methods that can be accessed within the class
and by subclasses but not from outside the class directly. In Python, protected members are
denoted by a single underscore ( _ ).

Example:
python Copy code

class Example: def __init__(self): self._protected_var = "I am protected"

3. Getter and Setter Methods


Access Control: Even though Python doesn't enforce strict private variables, encapsulation can
be managed by using getter and setter methods to control access to private variables. This
allows you to perform validation or restrictions on how values are set or retrieved.

Example:
python Copy code

class Example: def __init__(self, value): self.__private_var = value def


get_private_var(self): return self.__private_var def set_private_var(self,
value): if isinstance(value, str): self.__private_var = value else:
print("Only strings are allowed")

4. Name Mangling
Python performs name mangling for class variables and methods with a double underscore
prefix to make them less accessible from outside the class. This provides a basic form of
encapsulation, although not as strict as in other languages.
Example:
python Copy code

class Example: def __init__(self): self.__private_var = "Private" obj =


Example() print(obj._Example__private_var) # Accessing via name mangling

5. Encapsulation for Data Hiding


Data Hiding: The main aim of encapsulation is to hide the internal state of an object from the
outside. This allows the object to be protected from unwanted changes while providing
controlled access to the data through public methods.

6. Advantages of Encapsulation
Security: Encapsulation protects an object’s state by hiding its internal representation.
Flexibility: Internal data can be changed without affecting the external code interacting with
the object.
Controlled Access: Using getter and setter methods allows controlled access to the object's
properties, ensuring validation or other logic when changing the state.
Ease of Maintenance: Encapsulation leads to modular code, where each class can be modified
independently.

These concepts allow Python developers to structure their code in a way that promotes data
integrity, security, and simplicity in maintaining and modifying the application.

You might also like