Protected variable in Python


In programming the concept of protected variable is used to create an access control system. In this system, we establish a hierarchy or level of access control. This concept solidifies the basis of object-oriented programming. The technical meaning of a protected variable remains the same but the syntax and behaviour may vary between different programming languages.

Python follows the conventional logic of accessing and manipulating the attributes and methods, but the representation and degree of enforcement of access control is a little different.

Understanding Protected Variables in Python

Protected variables in python manages the internal use of the attributes and methods. These variables establish a convention that drives the logic of attribute access. In python, we can declare a protected variable with the help of an “underscore” (_) keyword.

These keywords are passed while declaring a method or a constructor within a class. We add the underscore keyword as a prefix before a variable to make it “protected”.

Syntax

Following is the syntax to declare a protected variable.

def __init__(self, player_name):
   self._player_name = player_name

Here, the “__init__()” method initializes the object’s attributes and is called whenever an object is created. We use the “self” keyword to access and manipulate the attributes and methods.

We create a protected variable to tell the other developers working on a codebase to treat this part of the code differently as its access is limited. Let’s discuss the significance of implementing protected variables in a code.

Significance of Protected Variables

The core idea behind the use of protected variables is to declare certain parts of the code as “protected” i.e., to prevent direct access of the variables from outside the environment. We can write the code in a meticulous manner and promote data encapsulation.

  • Protected variables help us to create different types functioning units within the class interface. Each functioning unit can have its own rules of implementation.

  • Protected variables provide controlled methods (getters and setters) that can access the hidden or encapsulated information within the class The code base becomes strong and readable as it allows the developers to treat different parts of code differently.

  • Protected variables also allow us to change the internal implementation details without affecting the external class/interface. Using protected variables, we can provide self-documents that can explain the instructions to interact with the class and its data.

Now that we have understood the fundamentals and significance of protected variables in python, let’s discuss an example and apply these concepts.

Understanding With the Help of an Example

We will consider an example, where we will code for a player profile management system. We will introduce protected variables to encapsulate the player information like: player name, Rating, team name and Salary. We will also use decorators (@property) to initialize the getter methods.

These methods will allow the coder to access the protected variables from outside and the decorators will prevent the internal data from being accidentally modified. A combination of protected variables and decorators will help us in creating a controlled interface.

We will also create a method which will modify the player Salary on the basis of their Rating. The class object will access the protected variables through the decorators providing an encapsulated Salary calculation logic.

Example

Following is an example of applying protected variables.

class PlayerProfile:
   def __init__(self, player_name, team_name, Rating):
      self._player_name = player_name
      self._team_name = team_name
      self._Rating = Rating
      self._Salary = 0
   @property
   def player_name(self):
      return self._player_name
   @property
   def team_name(self):
      return self._team_name
   @property
   def Rating(self):
      return self._Rating
   @property
   def Salary(self):
      return self._Salary
   def ManagingSalary(self):
      self._Salary = self._Rating * 200000
OBJ = PlayerProfile("Manoj Shukla", "Wolves", 8)
print(f"The player name is: {OBJ.player_name}")
print(f"The team name is: {OBJ.team_name}")
print(f"The player Rating is: {OBJ.Rating}")
OBJ.ManagingSalary()
print(f"The player Salary is: {OBJ.Salary} USD")

Output

The player name is: Manoj Shukla
The team name is: Wolves
The player Rating is: 8
The player Salary is: 1600000 USD

Comparision With Other Languages

The fundamental idea of controlling the access remains the same in all programming languages but the manner in which we implement the logic differ.

In most of the programming languages, the protected variables can only be accessed within the class hierarchy but python allows access of the protected variables from outside the class. Although, it is not encouraged as it is against the convention.

The key difference is in the declaration of the protected variables We explicitly use access modifiers/specifiers in C++ and Java, while in python we use the underscore keyword to declare a protected variable.

Conclusion

This article covers an important programming concept where we discussed the fundamentals of protected variables and its implementation in python. We discussed the programming convention of the class hierarchy and how protected variables help us in producing an encapsulated code. We further enhanced our understanding by discussing an example and drawing parallels with other programming languages.

Updated on: 19-Jan-2024

26 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements