Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Object-oriented Programmi...
  5. Access Modifiers in Python

Access Modifiers in Python

In object-oriented programming, access modifiers are used to control the visibility or accessibility of class members (attributes and methods).

  • These modifiers define how and where the attributes and methods of a class can be accessed.
  • Python, being a flexible and dynamically typed language, does not have strict access modifiers like other languages (such as Java or C++).
  • However, it provides certain conventions that control access to class members.

Why Use Access Modifiers in Python?

  • Encapsulation: Using access modifiers helps in hiding the internal state of an object and protects its data from unwanted access or modification.
  • Security: Private and protected members allow you to control access to sensitive or critical information, ensuring only authorized methods or classes can interact with it.
  • Code Organization: By marking certain attributes and methods as protected or private, you can communicate the intended use and organization of the class, making it easier to maintain and debug the code.

1.) Public Members:

  • Public members are accessible from anywhere, both inside and outside the class.
  • Public attributes and methods are declared without any special characters before them.

Example: In this case, both model and year attributes are public and can be accessed from outside the class. The display_info method is also public.

class Car:
    def __init__(self, model, year):
        self.model = model  # public attribute
        self.year = year    # public attribute

    def display_info(self):
        print(f"Model: {self.model}, Year: {self.year}")  # public method

car1 = Car("Tesla", 2022)
print(car1.model)  # Output: Tesla
car1.display_info()  # Output: Model: Tesla, Year: 2022

2.) Protected Members:

  • Protected members are intended to be accessible only within the class and by subclasses (derived classes). However, Python does not enforce this restriction strictly.
  • Protected members are indicated by a single underscore (_) prefix.

Example: The attributes _model and _year are protected and should not be directly accessed outside of the class or subclasses, but Python allows it. The method _display_info is also intended for internal use but is not strictly enforced as protected.

class Car:
    def __init__(self, model, year):
        self._model = model  # protected attribute
        self._year = year    # protected attribute

    def _display_info(self):
        print(f"Model: {self._model}, Year: {self._year}")  # protected method

class ElectricCar(Car):
    def display_info(self):
        print(f"Electric Car - Model: {self._model}, Year: {self._year}")

car1 = ElectricCar("Tesla", 2022)
car1.display_info()  # Output: Electric Car - Model: Tesla, Year: 2022

3.) Private Members:

  • Private members are intended to be accessible only within the class where they are defined. They cannot be accessed directly from outside the class.
  • Private members are indicated by a double underscore (__) prefix before the attribute or method name.

Example: The attributes __model and __year are private and cannot be accessed directly from outside the class. The __display_info method is also private. To access the private attributes, we provide a public method (get_model).

class Car:
    def __init__(self, model, year):
        self.__model = model  # private attribute
        self.__year = year    # private attribute

    def __display_info(self):
        print(f"Model: {self.__model}, Year: {self.__year}")  # private method

    def get_model(self):
        return self.__model  # Accessing private attribute through public method

car1 = Car("Tesla", 2022)
# print(car1.__model)  # This will raise an AttributeError
print(car1.get_model())  # Output: Tesla

How can we help?

Discussion 0

Join the Conversation

Your email address will not be published. Required fields are marked *