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 नमस्ते सदा वत्सले मातृभूमे त्वया हिन्दुभूमे सुखं वर्धितोहम् । महामङ्गले पुण्यभूमे त्वदर्थे पतत्वेष कायो नमस्ते नमस्ते ।।१।। प्रभो शक्तिमन् हिन्दुराष्ट्राङ्गभूता इमे सादरं त्वां नमामो वयम् त्वदीयाय कार्याय बध्दा कटीयं शुभामाशिषं देहि तत्पूर्तये । अजय्यां च विश्वस्य देहीश शक्तिं सुशीलं जगद्येन नम्रं भवेत् श्रुतं चैव यत्कण्टकाकीर्ण मार्गं स्वयं स्वीकृतं नः सुगं कारयेत् ।।२।। समुत्कर्षनिःश्रेयस्यैकमुग्रं परं साधनं नाम वीरव्रतम् तदन्तः स्फुरत्वक्षया ध्येय...

Khaleja Movie Logo

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

Search Engines Notice H1 Headings

What are H1 headings? An H1 heading is a prominent piece of text on a web page. It's like the headline of a newspaper or magazine article - it helps readers quickly understand what the web page is about. For example, if this email were a web page, "Search Engines Notice H1 Headings" would be its H1 heading. An H1 heading is plain text that lives between these HTML tags: How can H1 headings help my search engine rankings? Because headings are defined with HTML code, search engines understand that these pieces of text are of particular importance. You should include important keywords for a page in its H1 heading to tell search engines what the page is about, and help it rank higher in results for searches on those words. Do my website's pages have H1 headings? You can find out by looking at the HTML source code for every page on your site. When you're viewing the page, look for this option in your web browser. For example, in Firefox, you would clic...