#
Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

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 the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #