The Public private and protected access modifiers in Python determine the accessibility of class members such as attributes and methods. These access modifiers control how these members can be accessed and manipulated from within and outside the class. Here’s an explanation of each access modifier:
Now, we are going to discuss on public private and protected in python.
Public Access Modifier:
- Public members are accessible from anywhere, both within the class and from outside the class.
- By default, all class members (attributes and methods) are considered public if no access modifier is specified.
- Public members can be accessed using the dot notation (
object.member
). - Example:
class MyClass: def __init__(self): self.public_var = 10 # Public attribute def public_method(self): print("This is a public method") obj = MyClass() print(obj.public_var) # Output: 10 obj.public_method() # Output: This is a public method
Private Access Modifier:
- Private members are intended to be used within the class only and are not accessible from outside the class.
- Private members are denoted by prefixing the member name with double underscores (
__
). - Private attributes and methods can still be accessed within the class.
- Accessing private members from outside the class results in an
AttributeError
. - Example:
class MyClass: def __init__(self): self.__private_var = 20 # Private attribute def __private_method(self): print("This is a private method") def public_method(self): print(self.__private_var) # Accessing private attribute within the class self.__private_method() # Accessing private method within the class obj = MyClass() obj.public_method() # Output: 20\nThis is a private method print(obj.__private_var) # Raises AttributeError: 'MyClass' object has no attribute '__private_var' obj.__private_method() # Raises AttributeError: 'MyClass' object has no attribute '__private_method'
Protected Access Modifier:
- Protected members are intended to be used within the class and its subclasses (derived classes) and are not recommended for direct access from outside the class hierarchy.
- Protected members are denoted by prefixing the member name with a single underscore (
_
). - Protected members can be accessed from within the class and its subclasses, but not from outside the class hierarchy.
- Accessing protected members from outside the class hierarchy is allowed, but it’s considered a convention not to do so.
- Example:
class MyBaseClass: def __init__(self): self._protected_var = 30 # Protected attribute def _protected_method(self): print("This is a protected method") class MyDerivedClass(MyBaseClass): def access_protected(self): print(self._protected_var) # Accessing protected attribute in the derived class self._protected_method() # Accessing protected method in the derived class obj = MyDerivedClass() obj.access_protected() # Output: 30\nThis is a protected method print(obj._protected_var) # Output: 30 obj._protected_method() # Output: This is a protected method
Conclusion:
It’s important to note that Python’s access modifiers are more of a naming convention and not strictly enforced. The double underscore name mangling technique used for private members can still be accessed using the _ClassName__member
syntax, but it is not recommended. Protected members are also conventionally respected by developers,