Tutorial 6: OOPs in Python — Encapsulation-Protected attributes

Photo by Hitesh Choudhary on Unsplash

Missed previous lecture ?? — Click here

In this tutorial we will cover following topics :

Protected attribute

Property decorator

Property setter

Protected attribute

protected attributes are preceded by single underscore.

As we can see protected attributes can be accessed and modified outside class. To make them protected property decorator is used.

Property decorator

Let’s see Property decorator at work using example given below:

Using property decorator, modifying the attribute raises AttributeError and thus it is protected from being modified outside class.

Property setter

To be able to modify the protected attribute we use property setter as given below :

Using property setter now protected attribute can be modified.

Congratulations !!

You have completed fifth 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

Why Coding Your Own Makes You a Better Developer

Odoo — Understanding the Inverse Field Parameter

AN UNDERSTANDING OF DATA STRUCTURES AND ALGORITHMS.

How to Convert ARGB Integer into RGBA Tuple in Python

Resizing an EKS EBS volume safely in Kubernetes with blue-green

Native: THE Mobile-App-Development Trend to Watch in 2022

How we stress tested an app thousands pictures taken at the exact same time

Tutorial 7 : Regular Expressions in Python- understanding regex flags

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 7 : OOPs in Python — super() function

6 Awesome List Functions In Python

Python Dictionary Methods

Python Unknown (Tips and Tricks) — Part 1