Tutorial 5 : OOPs in Python — Encapsulation-Public, Private attributes

Photo by Hitesh Choudhary on Unsplash

Missed previous lecture ?? — Click here

In this tutorial we will cover following topics :

Encapsulation

Encapsulation as wrapping of data with methods in class

Encapsulation as access restriction

Name mangling

Encapsulation :

  • wrapping of data with methods operating on that data in class
  • restricting access to the data or methods that can manipulate it

Encapsulation as wrapping of data with methods in class :

Encapsulation as access restriction :

Objects can be -

pubic - accessible and modifiable outside class

private - not easily accessible and modifiable outside class

protected - objects can be accessible but can be made not modifiable outside class

NOTE : private attributes and private methods are preceded by two underscores

As you can see all the public attributes and public method can be accessed and modified outside class.

As you can see now private attribute and methods cannot be accessed outside the class — this is what I meant by access restriction using encapsulation.

NOTE : Accessing the private methods and private attributes outside class fetches AttributeError .

Name mangling :

Name mangling to bypass access restriction for private attributes and methods.

Use object._class__private_variable OR object._class__private_method to access the private attribute and call private method as shown below.

I hope now you have pretty good understanding of encapsulation. We will learn about protected attributes in next tutorial.

Take a deep breathe and move to the next tutorial.

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Architecture of Analytics Pipeline on Retail Banking Industry for Big Data

5 Root Cause Analysis Tools For Better Testing & QA

5 Root Cause Analysis Tools For Better Testing & QA Featured Image

Coding is Dangerous for Developers

Crack your Python Interview in the first attempt

How I took part as a technical writer in a blockchain project

I got Google MLE (L4) by practicing 200 LC questions(East Coast, 2021)

Binary Tree Traversals in Go

How to create an operating system for creativity and innovation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 4: OOPs in Python — Polymorphism

Class in Python : All you need to know

Numpy — slicing

Python Datatype : SET