Access Modifiers in Python
In Python, we can impose access restrictions on members (data members and member functions), e.g., which part of the class can be accessed from the outside, from within the class, or by a subclass. These restrictions are specified through access modifiers, which eventually keep the data and methods safe and organized. There are three types of access modifiers in Python, i.e., Public, Protected, and Private. Python does not enforce the access modifiers like other programming languages; it uses naming conventions instead to signal the intended access level.
Public Access Modifier
All members in Python are public by default. They can be accessed from anywhere in the program — inside the class, outside the class, and in subclasses.
class Person:
def __init__(self, name):
self.name = name #public attribute
def greet(self): #public method
print(f"Hello, I am {self.name}")
samplePerson = Person("Alice")
print(samplePerson.name) # -> allowed
samplePerson.greet() # -> allowedProtected Access Modifier
Get Nancyfazal’s stories in your inbox
Join Medium for free to get updates from this writer.
These members are intended for internal use, i.e., within a class and its subclass. A member is declared protected if its name starts with a single underscore (_). Python conventionally suggests that a protected member should not be called outside a class, but it does not prevent access.
class Person:
def __init__(self, name, citizen):
self._name = name #protected attribute
self._citizen = citizen #protected attribute
def _greet(self): #protected method
print(f"Hello, I am {self._name} and I am a {self._citizen} citizen.")
class Citizenship(Person):
def check(self):
# Accessing protected method inside subclass → Allowed
self._greet()
samplePerson = Citizenship("Alice", 'global')
samplePerson.check()
# Direct access from outside the class → Allowed but NOT recommended
samplePerson._greet()
print(samplePerson._name)
print(samplePerson._citizen)Private Access Modifier
These members can not be directly accessed from outside the class, but only from within the class. Their names are prefixed with double underscore(__). Python internally name-mangles these members, renaming them to _Classname__member. If a user believes that it is necessary to access a private property or method directly, they can do so using the mangled name (_Classname__member), although it is not recommended.
class Person:
def __init__(self, name, citizen):
self.__name = name #private attribute
self.__citizen = citizen #private attribute
def __greet(self): #private method
print(f"Hello, I am {self.__name} and I am a {self.__citizen} citizen.")
def display(self):
self.__greet()
samplePerson = Person("Alice", 'global')
samplePerson.display()
samplePerson.__greet() # -> ERROR: private method can not be accessed directly
samplePerson._Person__greet() # Accessing private method via manglingUltimately, access modifiers enables to hide the internal details, clarify class boundaries and write Python code that is easier to maintain and evolve over time.




