What are setter in OOPs in python

This recipe explains what are setter in OOPs in python

Recipe Objective

This recipe explains what is a setter.

Setter

We can define setter methods for any attribute using @property in Python. A special method that sets the value of a property is called setter. Setter helps in setting the value of a private attribute in a class. Generally, setter is used in data encapsulation.

Learn to use RNN for Text Classification with Source Code

# Setter
class student:
    def __init__(self, name, birth_year, age):
        self.name = name
        self.birth_year = birth_year
        self.age = age 
        
    @property    
    def email(self):
        return str(self.name) + "." + str(self.birth_year) + "@gmail.com"
    
    @email.setter
    def email(self, clg_email):
        name, birth_year = clg_email.split("@")[0].split(".")
        self.name = name
        self.birth_year = birth_year

Max = student("max", 2001, 20)
print(Max.email)

Max.email = "max.2001@gmail.com"
print(Max.__dict__)

 

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

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