Using access modifiers (public, protected, and private) is fundamental to encapsulation in OOP.
This is not just about Python but applicable to OOP in general.
However, when we talk specifically about Python, we notice that it fails to deliver true encapsulation procedures, which one would want to leverage in their OOP code.
How?
Let’s understand this today!
As you may already know, class attributes in OOP can be of three types:
A public member is accessible everywhere inside and outside the base class, and it is inherited by all child classes.
A protected member is accessible everywhere inside the base class and child class(es). It is not accessible outside the class.
A private member is accessible only inside the base class.
But, with Python, there are no such strict enforcements. This is unlike many other programming languages like C++.
Thus, protected members behave exactly like public members.
What’s more, private members can be (somehow) accessed outside the class as well.
For better clarity, consider the following class implementation:
Syntactically speaking, in Python:
A public member is declared with 0 leading underscores.
A protected member is declared with 1 leading underscore.
A private member is declared with 2 leading underscores.
Next, we instantiate a class object:
Moving on, as one would expect, the public attribute is accessible outside the class:
However, in a similar way, even the protected attribute is accessible outside the class, which, ideally, should not happen:
Lastly, when declaring private members, Python performs name mangling.
It is a technique used in programming to avoid naming conflicts between different classes.
Python performs name mangling by attaching underscore-prefixed class name (_MyClass
) to all members with two leading underscores.
So, while the private member is not directly accessible using its original name, it can still be accessed with the modified name obtained from name mangling.
This is demonstrated below:
As demonstrated in all the examples above, protected members and private members (with name mangling) can be accessed like public members.
Thus, the point that every Python programmer using OOP must remember here is that Python never enforces encapsulation.
Instead, leveraging encapsulation procedures in Python mainly relies on conventions.
They are used to communicate the accessibility protocols of class members to other programmers.
Thus, it is the programmer’s responsibility to obey these conventions.
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.
Also, we covered a template to develop quality code for machine learning development here: How to Structure Your Code for Machine Learning Development.
Moreover, once you have developed the pipeline, you must also test it, which we covered in detail here: Develop an Elegant Testing Framework For Python Using Pytest.
👉 Over to you: What are some other things to take care of in Python OOP?
For those wanting to develop “Industry ML” expertise:
At the end of the day, all businesses care about impact. That’s it!
Can you reduce costs?
Drive revenue?
Can you scale ML models?
Predict trends before they happen?
We have discussed several other topics (with implementations) in the past that align with such topics.
Here are some of them:
Learn sophisticated graph architectures and how to train them on graph data: A Crash Course on Graph Neural Networks – Part 1
Learn techniques to run large models on small devices: Quantization: Optimize ML Models to Run Them on Tiny Hardware
Learn how to generate prediction intervals or sets with strong statistical guarantees for increasing trust: Conformal Predictions: Build Confidence in Your ML Model’s Predictions.
Learn how to identify causal relationships and answer business questions: A Crash Course on Causality – Part 1
Learn how to scale ML model training: A Practical Guide to Scaling ML Model Training.
Learn techniques to reliably roll out new models in production: 5 Must-Know Ways to Test ML Models in Production (Implementation Included)
Learn how to build privacy-first ML systems: Federated Learning: A Critical Step Towards Privacy-Preserving Machine Learning.
Learn how to compress ML models and reduce costs: Model Compression: A Critical Step Towards Efficient Machine Learning.
All these resources will help you cultivate key skills that businesses and companies care about the most.
SPONSOR US
Get your product in front of 100,000 data scientists and other tech professionals.
Our newsletter puts your products and services directly in front of an audience that matters — thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., who have influence over significant tech decisions and big purchases.
To ensure your product reaches this influential audience, reserve your space here or reply to this email to ensure your product reaches this influential audience.