Access Modifiers in Python — Python Technical Interview Questions & Answers

Access Modifiers in Python

Access Modifiers in Python

  • Python uses naming conventions more than private keywords. normal_name is public. _name means “internal / subclass, please don’t touch” — convention only. __name inside a class triggers name mangling so outside access is harder. There is no true private like Java. Culture: we’re all adults, follow the underscore.
  • Viva: explain _, __, and that privacy is by convention. Don’t claim __ makes it encrypted. It only mangles the name.
  • Access Modifiers in Python — output: 1 2. pub and _hint readable. __hide is name-mangled — don’t touch from outside.

Technical Interview Tip: _ vs __ difference.

Code Examples for Access Modifiers in Python

Example

# Access modifiers
class A:
    def __init__(self):
        self.pub = 1
        self._hint = 2
        self.__hide = 3

a = A()
print(a.pub, a._hint)

Access Modifiers in Python — output: 1 2. pub and _hint readable. __hide is name-mangled — don’t touch from outside.

How to Explain Access Modifiers in Python to an Interviewer

Explain Access Modifiers as if you are teaching a junior — definition, then one tiny script.

Public: normal names _ protected convention

What does the example print, and why?

Access Modifiers in Python — output: 1 2

What mistake do freshers make with Access Modifiers?

__ name mangling