Make Dot Notation More Powerful with Getters and Setters
The elegance of dot notation combined with the power of getters and setters.
Dot notation provides a concise and elegant way to access and modify an objectโs attributes.
Yet, with dot notation, we can not validate the updates made to an attribute.
This means we can assign invalid values to an instanceโs attributes, as shown below:
One common way to avoid this is by defining a setter (set_side()
), which validates the assignment step.
But explicitly invoking a setter method isnโt as elegant as dot notation, is it?
Ideally, we would want to:
Use dot notation
and still apply those validation checks
The @๐ฉ๐ซ๐จ๐ฉ๐๐ซ๐ญ๐ฒ decorator in Python can help.
Hereโs how we can use it here.
First, define a getter as follows:
Declare a method with the attributeโs name.
Thereโs no need to specify any parameters for this method.
Decorate it with the @๐ฉ๐ซ๐จ๐ฉ๐๐ซ๐ญ๐ฒ decorator.
Next, define a setter as follows:
Declare a method with the attributeโs name.
Specify the parameter you want to update the attribute with.
Write the conditions as you usually would in any other setter method.
Decorate it with the @๐๐ญ๐ญ๐ซ๐ข๐๐ฎ๐ญ๐-๐ง๐๐ฆ๐.๐ฌ๐๐ญ๐ญ๐๐ซ decorator.
Done!
Now, you can use the dot notation while still having validation checks in place.
This is demonstrated below:
This approach offers both:
The validation and control of explicit setters and getters.
The elegance of dot notations.
Isnโt that cool?
Here are some interesting reads around Python OOP, which we have covered in the past and you must read next:
Python Does Not Fully Deliver OOP Encapsulation Functionalities
The Most Common Misconception About __init__() Method in Python
One of the Most Critical Pillars of OOP is Missing from Python
Also, hereโs a full deep dive into Python OOP if you want to learn more about advanced OOP in Python: Object-Oriented Programming with Python for Data Scientists.
๐ What are some cool things you know about Python OOP?
๐ If you liked this post, donโt forget to leave a like โค๏ธ. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights.
The button is located towards the bottom of this email.
Thanks for reading!
Latest full articles
If youโre not a full subscriber, hereโs what you missed last month:
A Beginner-friendly and Comprehensive Deep Dive on Vector Databases.
You Are Probably Building Inconsistent Classification Models Without Even Realizing
Why Sklearnโs Logistic Regression Has no Learning Rate Hyperparameter?
PyTorch Models Are Not Deployment-Friendly! Supercharge Them With TorchScript.
How To (Immensely) Optimize Your Machine Learning Development and Operations with MLflow.
DBSCAN++: The Faster and Scalable Alternative to DBSCAN Clustering.
Federated Learning: A Critical Step Towards Privacy-Preserving Machine Learning.
You Cannot Build Large Data Projects Until You Learn Data Version Control!
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
๐ Tell the world what makes this newsletter special for you by leaving a review here :)
๐ If you love reading this newsletter, feel free to share it with friends!
New to Python, could you put get_side and set_side? Having s.side for both makes me second guess my whole life ๐คฏ