What is data encapsulation in OOPs in python

This recipe explains what is data encapsulation in OOPs in python

Recipe Objective

This recipe explains what is data encapsulation.

Learn How to Build a Multi Class Text Classification Model using BERT

Data Encapsulation.

The process of wrapping up variables and methods into a single entity is known as Encapsulation. In the below program, we have defined a car class. In this car class __init__() method stores the maximum selling price of the car when we tried to modify the price we failed because python treats the __maxprice as private attributes. In order to change the value, we have to use a setter function i.e MaxPrice() which takes price as a parameter.

 

    class car:

    def __init__(self):
        self.__maxprice = 2000000

    def Sell(self):
        print("Selling Price: {}".format(self.__maxprice))

    def MaxPrice(self, price):
        self.__maxprice = price

    c = car()
    c.Sell()

    # change the price
    c.__maxprice = 2500000
    c.Sell()

    # using setter function
    c.MaxPrice(2500000)
    c.Sell()  
    

 

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.