Python — Encapsulation Does it exists??
Encapsulation is the packing of data and functions operating on that data into a single component and restricting the access to some of the object’s components.
Encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition.
A class is an example of encapsulation as it encapsulates all the data that is member functions,variables etc.
Difference between Abstraction and Encapsulation
Abstraction is a mechanism which represent the essential features without including implementation details.
Encapsulation: — Information hiding.
Abstraction: — Implementation hiding.
Encapsulation:
Python follows the philosophy of we’re all adults here with respect to hiding attributes and methods; i.e. you should trust the other programmers who will use your classes. Use plain attributes whenever possible.
You might be tempted to use getter and setter methods instead of attributes, but the only reason to use getters and setters is so you can change the implementation later if you need to. However, Python 2.2 and later allows you to do this with properties:
Protected members:
Protected member is (in C++ and Java) accessible only from within the class and it’s subclasses. How to accomplish this in Python? The answer is — by convention. By prefixing the name of your member with a single underscore, you’re telling others “don’t touch this, unless you’re a subclass”. See the example below:
Private members:
But there is a method in Python to define Private: Add “__” (double underscore ) in front of the variable and function name can hide them when accessing them from out of class.
Python doesn’t have real private methods, so one underline in the beginning of a method or attribute means you shouldn’t access this method.But this is just convention.I can still access the the variables with single underscore.
Also when using double underscore (__).we can still access the private variables
An example of accessing private member data.(Using name mangling)
class Person:
def __init__(self):
self.name = 'Manjula'
self.__lastname = 'Dube'
def PrintName(self):
return self.name +' ' + self.__lastname
#Outside class
P = Person()print(P.name)
print(P.PrintName())
print(P.__lastname)
#AttributeError: 'Person' object has no attribute '__lastname'
Note The __init__ method is a constructor and runs as soon as an object of a class is instantiated. Its aim is to initialize the object
Access public variable out of class, succeed
Access private variable our of class, fail
Access public function but this function access Private variable __B successfully since they are in the same class.
An example of accessing private member data.(Using name mangling technique)
class SeeMee:
def youcanseeme(self):
return 'you can see me'
def __youcannotseeme(self):
return 'you cannot see me'
#Outside class
Check = SeeMee()print(Check.youcanseeme())
# you can see meprint(Check.__youcannotseeme())
#AttributeError: 'SeeMee' object has no attribute '__youcannotseeme'
If you need to access the private member function
class SeeMee:
def youcanseeme(self):
return 'you can see me'
def __youcannotseeme(self):
return 'you cannot see me'
#Outside class
Check = SeeMee()print(Check.youcanseeme())
print(Check._SeeMee__youcannotseeme())
#Changing the name causes it to access the function
You can still call the method using its mangled name, so this feature doesn’t provide much protection.
You should know the following for accessing private members and private functions:
- When you write to an attribute of an object, that does not exist, the python system will normally not complain, but just create a new attribute.
- Private attributes are not protected by the Python system. That is by design decision.
- Private attributes will be masked. The reason is, that there should be no clashes in the inheritance chain. The masking is done by some implicit renaming. Private attributes will have the real name
"__<className>_<attributeName>"
With that name, it can be accessed from outside. When accessed from inside the class, the name will be automatically changed correctly.