Published: Aug. 11, 2019 by lukemakin |  estimated reading time: 6 minutes
Encapsulation is one of the fundamental rules of thumb in object oriented programming approach. It allows restricting access to methods and attributes and prevents the data being modified by accident. In Python it looks a little bit different than in languages such as Java, C, PHP etc. because there are no public, private, protected access modifiers. Instead the convention is to use a underscore or double underscores in the front. Let's look at our example to explore more about this topic:

class Galactic:

def __init__(self, name, size, distance):
self.__name = name
self.__size = size
self._distance = distance

def get_name(self):
return self.__name

def get_size(self):
return self.__size

def set_name(self, value):
self.__name = value

def set_size(self, value):
self.__size = value

def __myPrivateMethod(self):
return "this is a private method"

Single underscore indicates a private variable which shouldn't not be accessed directly. Let's create an object and look closer at "distance" attribute, which has a single underscore:


m31 = Galactic("Andromeda", 110, 2.54)

getattr(m31, "_distance")
'''
or
m31._distance
'''

setattr(m31, "_distance", 2.52)
'''
or
m31._distance = 2.52
'''

We created the great Andromeda Galaxy, distanced 2.54 million light years away from Earth, and in the example above we see that we can access the 'distance' variable directly and modify it (so we actually don't need getters and setters).

Let's look at name and size variables, that contain double underscores in the front.

getattr(m31, "name")

'''
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-82-ee4aa0e787fa> in <module>
----> 1 getattr(m31, "name")

AttributeError: 'Galactic' object has no attribute 'name'
'''

And now let's try to set attribute:


getattr(m31, "size")

'''
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-81-2d5a1b60791f> in <module>
----> 1 getattr(m31, "size")

AttributeError: 'Galactic' object has no attribute 'size'
'''

In the examples above, the two undescores have hidden variables  - when we tried to acces them we received an attribute error. So now if we would like to get the value (which is hidden) or change it - we should use the getters and setters.


m31.get_name()

'''
Output:
Andromeda
'''

m31.set_name("Andromeda Galaxy")

m31.get_name()

'''
Output:
Andromeda Galaxy
'''

Finally let's access the private method. The code for this is:  instance._Class__method


m31._Galactic__myPrivateMethod()

'''
Output:
this is a private method
'''

To sum up, in Python there are no public, private, protected keywords that are available in other programming languages. To use encapsulation we simply use underscores (single o double) to prevent messing our code by accident accessing attributes or methods.

 
Extras
To view additional content login or create a free account
Categories:
Share your thoughts

No comments yet...

Signup to the newsletter
To get the latest updates from pyplane
© copyright 2019 pyplane.com