π Python Access Modifiers β Public, Protected, Private Explained
π§² Introduction β Why Access Modifiers Matter
In object-oriented programming, encapsulation is the practice of restricting direct access to some components of an object, which is essential for maintaining data integrity and security. Although Python doesnβt enforce access restrictions like Java or C++, it provides naming conventions and name mangling to achieve similar functionality.
In this guide, you’ll learn:
- π The types of access modifiers in Python
- π How naming conventions influence access
- π Real-world examples using private and protected members
- π§ Best practices for encapsulation in Python
π Access Modifier Types in Python
Python provides three levels of access control using naming conventions:
Modifier | Syntax Example | Description |
---|---|---|
Public | self.name | Accessible from anywhere. |
Protected | self._name | Meant for internal use (not enforced). Accessible in subclasses. |
Private | self.__name | Name-mangled. Not directly accessible outside the class. |
π§ͺ Public Members
class Student:
def __init__(self, name):
self.name = name # public attribute
β Explanation:
self.name
is a public attribute.- Can be accessed and modified freely:
student.name = "Alice"
π Protected Members
class Student:
def __init__(self, name):
self._name = name # protected attribute
class Graduate(Student):
def display(self):
print("Name:", self._name)
β Explanation:
_name
is a protected member.- Can be accessed in subclasses but by convention shouldn’t be accessed directly outside.
π‘ Tip: Use _attribute
to indicate it’s intended for internal use only.
π Private Members with Name Mangling
class Student:
def __init__(self, name):
self.__name = name # private attribute
def get_name(self):
return self.__name
β Explanation:
__name
is a private member.- Python automatically changes
__name
to_Student__name
(name mangling). - Access:
student._Student__name
(not recommended).
β οΈ Warning: Private attributes are still accessible using mangled names. Pythonβs privacy is by convention, not enforced.
π Accessing Private Members (Name Mangling Example)
student = Student("Bob")
print(student._Student__name) # Output: Bob
β οΈ Note: Avoid this approach in production. Use getters/setters instead.
π§ Encapsulation Best Practices
Practice | Description |
---|---|
Use __attribute for private access | Prevents accidental access or override in subclasses |
Use _attribute for protected access | Indicates internal usage (e.g., during inheritance) |
Use public methods for access | Define get_ and set_ methods to control read/write access |
π§° Real-World Example
class BankAccount:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def get_balance(self):
return self.__balance
π This class:
- Hides the balance using
__balance
- Allows safe modification via
deposit()
- Prevents external tampering
π Summary β Recap & Next Steps
Python’s access modifiers rely on conventions rather than strict syntax rules. By using naming conventions such as _
and __
, developers can enforce encapsulation and prevent accidental misuse of class internals.
π Key Takeaways:
self.name
is publicself._name
is protected (by convention)self.__name
is private (via name mangling)- Access private attributes using getters/setters for safety
βοΈ Real-World Relevance: Encapsulation ensures robust, modular, and maintainable Python applications, especially in large codebases and collaborative projects.
β FAQ β Python Access Modifiers
β What is the purpose of access modifiers in Python?
β They help control access to class members, supporting encapsulation and maintaining object integrity.
β Are access modifiers enforced in Python?
β No, Python uses convention-based access. Even private attributes can be accessed externally via name mangling.
β How do I declare a private attribute?
β
Use double underscores: self.__myvar
. Python mangles it to _ClassName__myvar
.
β What is the difference between _var
and __var
?
β
_var
: Protected (by convention), accessible in subclass
β
__var
: Private (via name mangling), hidden from subclass and external access
β Can I access a private variable from outside the class?
β
Technically yes, using _ClassName__var
, but itβs not recommended.
Share Now :