When working with an object-oriented programming language like Python, encapsulation in Python is one of 4 important concepts to understand. The other three &this is inheritance , polymorphism a> and abstraction.
When working with classes and working with sensitive data, giving global access to all variables used in a program is not the best solution. Encapsulation in Python gives us the ability to access the variables we need without giving the program full access to any of those variables.
Updating, changing, or deleting data from variables can be done using methods defined specifically for this purpose. The advantage of using this programming approach is &improved input control and security.
The concept of encapsulation is the same for all OO languages programming. The difference is visible when the concepts are applied to specific languages.
Compared to languages like Java, which offer access modifiers (public or private) for variables and methods, Python provides access to all variables and methods. < / p>
The code below shows how easy it is to access variables.
class Person: def __init __ (self, name, age = 0): self.name = name self.age = age def display (self): print (self.name) print (self.age) person = Person (`Dev`, 30) #accessing using class method person.display () #accessing directly from outside print (person.name) print (person.age)
Dev 30 Dev 30
Since we have no access modifiers in Python, we will use several different methods to control access to variables in a program.
Python proposes n Several methods to restrict access to variables and methods in the program. Let`s take a closer look at the methods.
A common Python programming convention for identifying a private variable is &it is an underscore prefix. Now it doesn`t make any difference to the compiler. The variable is still available normally. But because it`s a programmer convention, it tells other programmers that variables or methods should only be used within the class.
class Person: def __init __ (self, name, age = 0): self.name = name self._age = age def display (self): print (self.name) print (self._age) person = Person (`Dev` , 30) #accessing using class method person.display () #accessing directly from outside print (person.name) print (person._age)
Dev 30 Dev 30
It`s clear that access to variables did not change. But can we do anything to make it really private? Let`s see next.
If you want to make class members, that is, methods and variables, private, you must prepend them with a double underscore. But Python offers some support for the private modifier. This mechanism is called name change. However, you can still access the class members from the outside.
In Python, any identifier with __Var is rewritten by the python interpreter as _Classname__Var, and the class name remains as the current class name. This mechanism for renaming is called renaming.
In the example below, in the Class person, the age variable has been changed and is preceded by double underscores at the beginning.
class Person: def __init __ (self, name, age = 0): self.name = name self .__ age = age de f display (self): print (self.name) print (self .__ age) person = Person (`Dev`, 30) #accessing using class method person.display () #accessing directly from outside print (` Trying to access variables from outside the class `) print (person.name) print (person .__ age)
Dev 30 Trying to access variables from outside the class Dev Traceback (most recent call last): File " Person.py ", line 16, in & lt; module & gt; print (person .__ age) AttributeError: `Person` object has no attribute` __age`
You may notice that variables can still be accessed using methods that are part of the class. But you cannot access directly from the outside as it is a private variable.
If you want to access and modify private variables, you should use getter and setter accessors as they are part of Class.< pre class = "EnlighterJSRAW" data-enlighter-language = "python"> class Person: def __init __ (self, name, age = 0): self.name = name self .__ age = age def display (self): print (self .name) print (self .__ age) def getAge (self): print (self .__ age) def setAge (self, age): self .__ age = age person = Person (`Dev`, 30) #accessing using class method person .display () #changing age using setter person.setAge (35) person.getAge ()
Dev 30 35
Encapsulation not only provides better data flow, but also protects data e from external sources. The concept of encapsulation makes the code self-contained.
This is very useful at the implementation level, you should hide the data in a block to make encapsulation easier and also protect the data.
The following reasons show why developers find encapsulation convenient and why the object-oriented concept is superior to many programming languages.
Encapsulation in Python is that the data is hidden outside of the object`s definition. This allows developers to create a user-friendly interface. It also helps protect your data from hacking, as the code is highly secure and not available to external sources.