Python access modifiers (public + private + protected variables)
// updated 2025-11-05 16:23
When programming in general, we can limit certain variables and functions to a specific scope; we call this access modification and usually divide it up into three different access types:
- public (default)
- private
- protected
Public access modifier
By default, all variables and functions that remain unmarked have a public access modifier: any function outside the scope can access them:
class Person:
def __init__(self, name, age):
# all these variables are public
self.name = name
self.age = age
# this function is public too
def display_age_next_year(self):
print(self.age + 1)
jon = Person("Jon", 100)
# accessing a function from outside the class
jon.display_age_next_year()
# Output
# 101So, what would a private access modification look like?
Private access modifier
A private access modifier consists of two underscores (__) before a variable name; any attempt to access that variable outside its scope will result in an error:
class Person:
def __init__(self, name, age):
# this variable is public
self.name = name
# but this variable is private
self.__age = age
jon = Person("Jon", 100)
print(jon.name)
print(jon.__age)
# Output
# Jon
# (AttributeError)As we see up there, we can access the name variable, but not the age from outside the class' scope ... yet, we can still access the age variable from inside the class' scope:
class Person:
def __init__(self, name, age):
# this variable is public
self.name = name
# but this variable is private
self.__age = age
def get_age_next_year(self):
# we can access __age from here since we are still in scope
return self.__age + 1
jon = Person("Jon", 100)
# access the age variable only via the function
print(jon.get_age_next_year())
# Output
# 101This example might seem "roundabout" as in "why do we need to have that age-getting function when we can just make the age variable public?" but sometimes we just wish to hide the original variable(s) while showing only the final result of a function!
In a closed source program, maybe we simply don't want other programmers to have access to the original variables?
Protected access modifier
We denote the protector access modifier with a single underscore before the variable name (similar to how we denoted the private access modifier with a double underscore):
# creating a superclass
class Person:
def __init__(self, name, age):
# both these variables are protected
self._name = name
self._age = age
# creating a subclass of the class Person
class Student(Person):
# creating public functions
# that can access the superclass' protected variables
def get_name(self):
return self._name
def get_age(self):
return self._age
my_character = Student("Codebear", 99)
print(my_character.get_name())
print(my_character.get_age())
# Output (with no AttributeError):
# Codebear
# 99So what differentiates the protected from the private? A protected access modifier prevents any code from outside the class scope from accessing the class variables and functions, unless the code comes from a subclass (we shall look further into subclasses when we look into the principles of class inheritance)!