Skip to main content

Isn't there any access modifiers like public, private, protected in python ??

 

Python does not have explicit access modifiers like public, private, and protected as in some other languages like Java or C++. Instead, it relies on naming conventions to indicate the intended level of access. Here's how it works:

1. Public (default)

Any attribute or method is public by default. This means it can be accessed from anywhere in the code.

 class MyClass:
    def __init__(self):
        self.public_var = "I am public"

obj = MyClass()
print(obj.public_var)  # Accessible from anywhere

2. Protected (indicated by a single underscore _)

By convention, attributes or methods with a leading underscore are considered protected, meaning they are intended to be used only within the class and its subclasses. This is a convention and not enforced by the language.

class MyClass:
    def __init__(self):
        self._protected_var = "I am protected"

obj = MyClass()
print(obj._protected_var)  # Technically accessible, but it's a convention not to use it directly
 

3. Private (indicated by double underscores __)

Attributes or methods with a leading double underscore are private. Python implements name mangling to make these attributes harder to access from outside the class, but they are still not strictly private (you can still access them with some effort).

class MyClass:
    def __init__(self):
        self.__private_var = "I am private"

obj = MyClass()
# print(obj.__private_var)  # AttributeError: 'MyClass' object has no attribute '__private_var'
print(obj._MyClass__private_var)  # Accessible using name mangling
 

Summary of Access Levels

ModifierSyntaxAccessibility
Publicself.varFully accessible everywhere
Protectedself._varAccessible but intended for internal use
Privateself.__varName mangled, harder to access, but not fully private

Pythonic Philosophy

Python follows a philosophy of "we are all consenting adults here", meaning developers are trusted to follow conventions rather than being strictly prevented from accessing certain attributes. This provides flexibility but requires discipline to respect these conventions.

 

 

 

Comments

Popular posts from this blog

Namaste Sada Vatsale - RSS Prayer in English

  Namaste Sada Vatsale is Rashtriya Swayamsevak Sangh's prayer. This prayer is in Sanskrit except last line which is in Hindi. It is compulsory to sing this prayer in all programs of Sangh. It was written by some unknown Sanskrit professor in guidance of Dr. K. B. Hedgewar and Madhav Sadashiv Golwalkar. Originally this poem was written in Sanskrit language. Later it was translated in Hindi also. Its original form in other Indian script as well as IOAT format is given as under: RSS Prayer in Devanagari नमस्ते सदा वत्सले मातृभूमे त्वया हिन्दुभूमे सुखं वर्धितोहम् । महामङ्गले पुण्यभूमे त्वदर्थे पतत्वेष कायो नमस्ते नमस्ते ।।१।। प्रभो शक्तिमन् हिन्दुराष्ट्राङ्गभूता इमे सादरं त्वां नमामो वयम् त्वदीयाय कार्याय बध्दा कटीयं शुभामाशिषं देहि तत्पूर्तये । अजय्यां च विश्वस्य देहीश शक्तिं सुशीलं जगद्येन नम्रं भवेत् श्रुतं चैव यत्कण्टकाकीर्ण मार्गं स्वयं स्वीकृतं नः सुगं कारयेत् ।।२।। समुत्कर्षनिःश्रेयस्यैकमुग्रं परं साधनं नाम वीरव्रतम् तदन्तः स्फुरत्वक्षया ध्येय...

Miklós Fehér - Soccer player who died during the match

Miklós "Miki" Fehér (20 July 1979 — 25 January 2004) was an Hungarian footballer who played as a striker. On 25 January 2004, Fehér died of a cardiac arrest during a match between Vitória de Guimarães and his team Benfica in Guimarães, Portugal. On 25 January 2004, Benfica travelled to Guimarães to play against Vitória de Guimarães. The game was being broadcast live on television, and Benfica were leading 1–0. Fehér had just come on as a substitute, and assisted another player brought from the bench, Fernando Aguiar, for the match's only goal, but received a yellow card in injury time and suddenly bent forward, seemingly in pain. He then fell backwards to the ground. Members of both teams rushed immediately to aid Fehér before medical personnel arrived on the pitch. CPR was performed as match participants looked on in visible distress. An ambulance arrived on the pitch and Fehér was rushed to the hospital. His condition was covered by the Portuguese media througho...

Khaleja Movie Logo

Mahesh Babus New movie Khaleja Logo released ... its taken from His twitter account Image courtesy: Twitpic