#
CodeCrafters IT Innovation | Bansal Computer Center, Dhuri – IT & AI Courses
Loading...

Python Access Modifiers

Hello

Hello

Hello

🧠 What are "Access Modifiers"?

Hello

Access modifiers control who can access variables or methods. In Python, they are conventions, not strict rules.

Hello

🧩 Let's Understand the Three Types

βœ… Public

Hello

⚠️ Protected

Hello

πŸ” Private

Hello

Hello

Naming Conventions

Hello

Hello

Name Style Meaning
name Public
_name Protected
__name Private

πŸ“Œ But Python does not force you to follow these. It just gives a signal.

Hello

Name Mangling

Hello

Hello

dir() Function

Hello

Hello

Common Mistake

Hello
class abc:
    def class1(self):
        self._a = 10

a1 = abc()
print(abc._a)  # ❌ Error

Fix:

a1.class1()
print(a1._a)  # βœ… Works

Hello

Summary Table

Hello

Term Meaning Example Notes
Public Accessible anywhere self.name Safe
Protected For subclass only self._name Not enforced
Private Inside class only self.__name Name mangled
Name Mangling _ClassName__name obj._A__secret Obfuscation
dir() List attributes dir(obj) Shows mangled names

Hello

What Students Say

Here are our students...

Phone

Contact Us

Empower your tech dreams with us. Get in touch!

  • Partner brand
  • Partner brand
  • Partner brand
  • Partner brand
  • Partner brand