Encapsulation

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
Search for a command to run...

👋 Hi there, I am Nirmal. I am a former software engineer with a keen interest in data science and analytics domains. Besides, I love to contribute to open source and help others to understand various tech stuffs.
No comments yet. Be the first to comment.
This series will provides the core concepts of python programming starting from variables, data types, control flow, and functions to some problem solving.
In Python, an iterator is an object that enables traversal over a collection of items, one at a time. It follows the iterator protocol, which involves implementing the __iter__() and __next__() methods. The __iter__() method returns the iterator ob...
Bias in Machine Learning Bias refers to the simplifying assumptions made by a model to make the target function easier to learn. High bias can lead to underfitting Represents the error introduced by approximating a real-world problem Example: A li...
When it comes to developing software applications, choosing the appropriate architectural style is a critical decision. Among the popular options are microservices and monolithic architecture. By understanding the trade-offs, readers can make well-i...

In a world where information is everywhere, businesses have tons of data at their fingertips. This data is like a goldmine waiting to be used for smart decisions. That's where Big Data analytics and Data Science come in. They're like the experts who ...

Python is a powerful tool for data professionals, largely due to its extensive collection of open-source libraries and packages. In this blog, we'll explore the fundamentals of pandas, with a special focus on its core data structures: Series and Data...

NumPy is a powerful library for numerical computing in Python, offering robust support for large, multi-dimensional arrays and an extensive collection of mathematical functions. Creating Arrays To begin using NumPy, import the library and create arra...

Encapsulation is an essential concept in object-oriented programming that promotes data hiding and helps maintain code integrity.
In Python, although there are no strict access modifiers like in some other languages, developers can use naming conventions to indicate the intended visibility of class members.
Private members in Python are denoted by a double underscore (__) prefix before the variable or method name.
By convention, this prefix indicates that the member is intended for internal use within the class and should not be accessed directly from outside.
Example:
class MyClass:
def __init__(self):
self.__private_var = 100
def __private_method(self):
print("This is a private method.")
def public_method(self):
self.__private_method()
print(f"The private variable value is: {self.__private_var}")
obj = MyClass()
obj.public_method()
This is a private method.
The private variable value is: 100
In the above example, __private_var and __private_method are private members of the MyClass class. They are accessed within the class using the double underscore prefix. If we want to access the private variable from outside like obj.__private_method() then it shows AttributeError
Protected members in Python are denoted by a single underscore (_) prefix before the variable or method name.
By convention, this prefix suggests that the member is intended to be used within the class and its subclasses.
Though protected members can still be accessed from outside the class, developers should treat them as non-public, discouraging direct access.
Example:
class BaseClass:
def __init__(self):
self._protected_var = 10
def _protected_method(self):
print("This is a protected method.")
class SubClass(BaseClass):
def access_protected(self):
print(f"The protected variable value is: {self._protected_var}")
self._protected_method()
obj = SubClass()
obj.access_protected()
In this example, _protected_var and _protected_method are protected members within the BaseClass. They are accessible within the class and its subclasses.