Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Protected | Encapsulation
In-Depth Python OOP

Protected

The protected access modifier can be used in the global scope, but it is generally considered to be a bad practice. IDEs typically hide protected attributes and methods from outside the class, which can hinder autocomplete and code correction.

Code Description
This Python code demonstrates the concept of protected attributes and methods in a class.

The SomeClass class is defined, and it has two attributes: first and _attribute. The first attribute is a public attribute, meaning it can be accessed from outside the class. On the other hand, the _attribute attribute is marked as protected by convention, as it has a single underscore prefix. Protected attributes are intended to indicate that they should not be accessed or modified directly from outside the class, although Python does not enforce this restriction.

The class also defines two methods: _method and info. The _method method is marked as protected by convention, using the underscore prefix. Similarly, this indicates that the method is intended for internal use within the class and should not be called from outside the class. Again, Python does not enforce this restriction.

The info method is a public method that provides information about the attributes and invokes the protected _method method. It prints the string "INFO" and then outputs the values of first and _attribute attributes using print("first:", self.first) and print("_attribute:", self._attribute) respectively. Finally, it calls the _method method using self._method().

The code creates an instance of the SomeClass class using instance = SomeClass(). It then demonstrates both good and bad practices related to accessing protected attributes and methods. It first prints the value of the _attribute attribute directly using print("Outside:", instance._attribute). Although it works, directly accessing a protected attribute from outside the class is generally discouraged as per the convention.

Next, it calls the info method on the instance using instance.info(). This is considered an acceptable practice as it is an intentional invocation of a public method that internally uses the protected attribute and method. The info method prints the desired information and calls the _method method as expected.

In summary, this code illustrates the usage of protected attributes and methods in Python classes. It showcases how they are conventionally marked with a single underscore prefix to indicate that they are intended for internal use and should not be accessed directly from outside the class, although Python does not enforce this convention.

You can use protected attributes and methods inside subclasses.

Code Description
The code defines a base class called User, which has a protected attribute _entity set to the string value "Internet User".

The code also defines a subclass called Admin, which inherits from the User class. The Admin class has a method called print_entity, which is responsible for printing the value of the protected attribute _entity using print(self._entity).

An instance of the Admin class is created using admin = Admin(), and the print_entity method is called on this instance using admin.print_entity().

Since the Admin class is a subclass of the User class, it inherits the protected attribute _entity. As a result, when the print_entity method is called on the admin instance, it is able to access and print the value of the protected attribute _entity without any issues.

Therefore, the code demonstrates that you can indeed use protected attributes and methods inside subclasses, as the protected attribute _entity is accessible and can be printed within the print_entity method of the Admin subclass.

The protected access modifier allows you to define logic inside a class that can be extended to subclasses but should not be accessed directly from outside the class hierarchy.

Note

Protected attributes/methods in Python work similarly to regular attributes/methods, but by convention among developers, they are used for encapsulating data within a class and its subclasses.

How to define protected attribute?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
course content

Course Content

In-Depth Python OOP

Protected

The protected access modifier can be used in the global scope, but it is generally considered to be a bad practice. IDEs typically hide protected attributes and methods from outside the class, which can hinder autocomplete and code correction.

Code Description
This Python code demonstrates the concept of protected attributes and methods in a class.

The SomeClass class is defined, and it has two attributes: first and _attribute. The first attribute is a public attribute, meaning it can be accessed from outside the class. On the other hand, the _attribute attribute is marked as protected by convention, as it has a single underscore prefix. Protected attributes are intended to indicate that they should not be accessed or modified directly from outside the class, although Python does not enforce this restriction.

The class also defines two methods: _method and info. The _method method is marked as protected by convention, using the underscore prefix. Similarly, this indicates that the method is intended for internal use within the class and should not be called from outside the class. Again, Python does not enforce this restriction.

The info method is a public method that provides information about the attributes and invokes the protected _method method. It prints the string "INFO" and then outputs the values of first and _attribute attributes using print("first:", self.first) and print("_attribute:", self._attribute) respectively. Finally, it calls the _method method using self._method().

The code creates an instance of the SomeClass class using instance = SomeClass(). It then demonstrates both good and bad practices related to accessing protected attributes and methods. It first prints the value of the _attribute attribute directly using print("Outside:", instance._attribute). Although it works, directly accessing a protected attribute from outside the class is generally discouraged as per the convention.

Next, it calls the info method on the instance using instance.info(). This is considered an acceptable practice as it is an intentional invocation of a public method that internally uses the protected attribute and method. The info method prints the desired information and calls the _method method as expected.

In summary, this code illustrates the usage of protected attributes and methods in Python classes. It showcases how they are conventionally marked with a single underscore prefix to indicate that they are intended for internal use and should not be accessed directly from outside the class, although Python does not enforce this convention.

You can use protected attributes and methods inside subclasses.

Code Description
The code defines a base class called User, which has a protected attribute _entity set to the string value "Internet User".

The code also defines a subclass called Admin, which inherits from the User class. The Admin class has a method called print_entity, which is responsible for printing the value of the protected attribute _entity using print(self._entity).

An instance of the Admin class is created using admin = Admin(), and the print_entity method is called on this instance using admin.print_entity().

Since the Admin class is a subclass of the User class, it inherits the protected attribute _entity. As a result, when the print_entity method is called on the admin instance, it is able to access and print the value of the protected attribute _entity without any issues.

Therefore, the code demonstrates that you can indeed use protected attributes and methods inside subclasses, as the protected attribute _entity is accessible and can be printed within the print_entity method of the Admin subclass.

The protected access modifier allows you to define logic inside a class that can be extended to subclasses but should not be accessed directly from outside the class hierarchy.

Note

Protected attributes/methods in Python work similarly to regular attributes/methods, but by convention among developers, they are used for encapsulating data within a class and its subclasses.

How to define protected attribute?

Select the correct answer

Everything was clear?

Section 3. Chapter 3
some-alt