Access Modifiers
Access modifiers control which parts of your program can read or change an object's data. Python uses naming conventions rather than keywords.
Public (default)
class Person:
def __init__(self, name):
self.name = name # public — accessible anywhere
p = Person("Alice")
print(p.name) # → Alice ✓No underscore = public. Anyone can read or write it.
Protected (_single underscore)
class Employee:
def __init__(self, salary):
self._salary = salary # protected convention
e = Employee(50000)
print(e._salary) # → 50000 (works, but signals caution)A single leading underscore is a convention. Python will not prevent access, but it signals to other developers: "this is internal — do not use directly." In IB exams, _attribute = protected.
Private (__double underscore)
class Employee:
def __init__(self, pin):
self.__pin = pin # private — name-mangled
e = Employee(1234)
# e.__pin # → AttributeError!
# e._Employee__pin # the mangled name (avoid using this)Double underscore triggers name mangling. Python renames the attribute to _ClassName__attribute internally, making accidental access from outside the class raise an AttributeError.
Comparison: Java vs Python
- Java uses keywords:
public,protected,private— enforced by the compiler - Python uses naming conventions:
name,_name,__name— enforced by convention - IB questions may use either language — the concept is the same
Your Task
- Rename
self.salarytoself._salary(protected) - Rename
self.pintoself.__pin(private) - Update
__init__so both assignments use the new names. Theget_detailsmethod already usesself._salary— leave that as is.
After your changes, e._salary should return 50000 and e.__pin should raise an AttributeError.
Public (default)
No underscore = public. Anyone can read or write it.
Access Modifiers
Access modifiers control which parts of your program can read or change an object's data. Python uses naming conventions rather than keywords.
Public (default)
class Person:
def __init__(self, name):
self.name = name # public — accessible anywhere
p = Person("Alice")
print(p.name) # → Alice ✓No underscore = public. Anyone can read or write it.
Protected (_single underscore)
class Employee:
def __init__(self, salary):
self._salary = salary # protected convention
e = Employee(50000)
print(e._salary) # → 50000 (works, but signals caution)A single leading underscore is a convention. Python will not prevent access, but it signals to other developers: "this is internal — do not use directly." In IB exams, _attribute = protected.
Private (__double underscore)
class Employee:
def __init__(self, pin):
self.__pin = pin # private — name-mangled
e = Employee(1234)
# e.__pin # → AttributeError!
# e._Employee__pin # the mangled name (avoid using this)Double underscore triggers name mangling. Python renames the attribute to _ClassName__attribute internally, making accidental access from outside the class raise an AttributeError.
Comparison: Java vs Python
- Java uses keywords:
public,protected,private— enforced by the compiler - Python uses naming conventions:
name,_name,__name— enforced by convention - IB questions may use either language — the concept is the same
Your Task
- Rename
self.salarytoself._salary(protected) - Rename
self.pintoself.__pin(private) - Update
__init__so both assignments use the new names. Theget_detailsmethod already usesself._salary— leave that as is.
After your changes, e._salary should return 50000 and e.__pin should raise an AttributeError.