Python classes allow us to organize code by encapsulating related data and functions. In some cases, we might want to prohibit the use of variables from outside the class. Python allows to have private variables in classes to stop users of the code from accessing those variables. Private variables are defined by having 2 underscores at the beginning of the variable name (__). While public variables can be directly accessed outside the class, private variables cannot. Let's illustrate this:
self.public_var = 'I am public!' # Public variable
self.__private_var = 'I am private!' # Private variable
example = PublicPrivateExample()
print(example.public_var) # 'I am public!'
print(example.__private_var) # Raises AttributeError
In this case, we can see that the public variable is easily accessible. However, if we try to access the private variable, Python will raise an AttributeError.
Python doesn't have strict private members like other languages such as Java or C++. In Python, a private member is simply a convention that suggests not to access the member directly. Python uses name mangling to make it less likely to access or modify a private variable accidentally. However, you still can access it if you need, though it's generally discouraged:
print(example._PublicPrivateExample__private_var) # I am private!
The access to the private variable is performed using _ClassName__private_var. This discourages its direct access and encourages encapsulation. You should provide public methods if you want to provide access to private variables:
self.__private_var = 'I am private!'
def get_private_var(self): # Getter method for private variable
return self.__private_var # We can access private fields inside the class
example = PublicPrivateExample()
print(example.get_private_var()) # Will print 'I am private!'
Here we use a public method get_private_var to provide a controlled way to access our private variable. This way, we maintain the encapsulation and integrity of our data.
Keep in mind that using private variables is part of the practice of encapsulation, which is one of the fundamentals of Object-Oriented Programming. It protects the data from being changed and accessed unexpectedly.
So, even though Python doesn't strictly enforce private variables, it's good practice to use them to make your code more maintainable and error-free.
Challenge: Database Class
You are asked to develop a Database class in Python that stores name: value pairs. The database should be represented as a private dictionary inside the Database class.
The Database class should have the following methods:
insert(name, value): This method should insert a new name: value pair into the database. If the name already exists in the database, it should print a message saying, Name already exists in the database..
delete(name): This method should delete an existing name: value pair from the database. If the name does not exist, it should print a message saying, Name does not exist in the database..
update(name, value): This method should update the value of an existing name in the database. If the name does not exist, it should print a message saying, Name does not exist in the database..
display(): This method should display all the name: value pairs in the database in the format name: value. All the values should be separated by a comma with a space (, ).