Skip to main content

Posts

Showing posts from January, 2025

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() pr...