0% found this document useful (0 votes)
12 views3 pages

Encapsulation - Jupyter Notebook

The document explains the concepts of encapsulation and abstraction in Python, detailing how to use protected and private members in classes. It illustrates these concepts with code examples, demonstrating how to access protected and private attributes. The document emphasizes the importance of restricting access to sensitive data within classes.

Uploaded by

subhampattnaik82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Encapsulation - Jupyter Notebook

The document explains the concepts of encapsulation and abstraction in Python, detailing how to use protected and private members in classes. It illustrates these concepts with code examples, demonstrating how to access protected and private attributes. The document emphasizes the importance of restricting access to sensitive data within classes.

Uploaded by

subhampattnaik82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/1/24, 11:43 AM encapsulation_abstraction - Jupyter Notebook

Encapsulation : It is a mechanism of wrapping the data (variables)


and code acting on the data (methods) together as a single unit.
Encapsulation is used to restrict access to methods and
variables.

1. Protected member : Python's convention to make an instance variable protected is


to add a prefix _ (single underscore) to it. The attribute/method can be used in the
class where it is defined and can be accessed outside of a class using same class's
object. ¶

2. Private member : Python's convention to make an instance variable protected is to


add a prefix __(double underscore) to it. Private members are the same as protected
members. The difference is that class members who have been declared private
should not be accessed by anyone outside the class.

In [1]: 1 class Person:


2 _name = 'XYZ'
3 __pswd = 'Abcd@123'
4
5 def display(self):
6 print('Protected member:',self._name)
7 print('Private member:',self.__pswd)

In [2]: 1 # Accessing protected member through class's object


2 per = Person()
3 per._name

Out[2]: 'XYZ'

In [3]: 1 # Accessing protected member through class's name


2 Person._name

Out[3]: 'XYZ'

In [4]: 1 # Accessing protected member through class's method


2 per.display()

Protected member: XYZ


Private member: Abcd@123

In [5]: 1 # Accessing private member through class's object


2 per.__pswd

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
Cell In[5], line 2
1 # Accessing private member through class's object
----> 2 per.__pswd

AttributeError: 'Person' object has no attribute '__pswd'

localhost:8888/notebooks/Desktop/PYTHON/16th Nov Python/encapsulation_abstraction.ipynb 1/3


4/1/24, 11:43 AM encapsulation_abstraction - Jupyter Notebook

In [6]: 1 # Accessing private member through class's name


2 Person.__pswd

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
Cell In[6], line 2
1 # Accessing private member through class's name
----> 2 Person.__pswd

AttributeError: type object 'Person' has no attribute '__pswd'

Abstraction:- making attributes private.


Any attribute that is not accessible by the object is called a priv
ate attribute.
Any attribute that starts with a double underscore is called a priv
ate attribute.

In [7]: 1 class Credential:


2 def __init__(self,username,password):
3 self.username = username
4 self.password = password
5
6 def show(self):
7 print('Username:',self.username)

In [8]: 1 cred = Credential('Hello','hello@123')

In [9]: 1 cred.username

Out[9]: 'Hello'

In [10]: 1 cred.password

Out[10]: 'hello@123'

The password attribute can be made private as below 👇:-

In [12]: 1 class Credential1:


2 def __init__(self,username,password):
3 self.username = username
4 self.__password = password
5
6 def show(self):
7 print('Username:',self.username)

localhost:8888/notebooks/Desktop/PYTHON/16th Nov Python/encapsulation_abstraction.ipynb 2/3


4/1/24, 11:43 AM encapsulation_abstraction - Jupyter Notebook

In [13]: 1 cred1 = Credential1('Hello','hello@123')

In [14]: 1 cred1.username

Out[14]: 'Hello'

In [15]: 1 cred1.__password

--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
Cell In[15], line 1
----> 1 cred1.__password

AttributeError: 'Credential1' object has no attribute '__password'

In [16]: 1 cred1.show()

Username: Hello

In [ ]: 1 ​

localhost:8888/notebooks/Desktop/PYTHON/16th Nov Python/encapsulation_abstraction.ipynb 3/3

You might also like