Encapsulation

Encapsulation:

In an object oriented python program, you can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as encapsulation.

To implement encapsulation, we need to learn how to define and use a private attribute and a private function.

Other programming languages have protected class methods too, but Python does not.

Encapsulation gives you more control over the degree of coupling in your code, it allows a class to change its implementation without affecting other parts of the code.

Type

Description

public methods

Accessible from anywhere

private methods

Accessible only in their own class. starts with two underscores

public variables

Accessible from anywhere

private variables

Accesible only in their own class or by a method if defined. starts with two underscores

 

 

 

Protected Attribute and Method

If you have read some Python code, you will always find some attribute names with a prefixed underscore. Let’s write a simple Class:

class Base(object):

    def __init__(self):

        self.name = "hello"

        self._protected_name = "hello_again"

    def _protected_print(self):

        print "called _protected_print"

b = Base()

print b.name

print b._protected_name

b._protected_name = "new name"

print b._protected_name

b._protected_print()


The output will be:
hello
hello_again
new name
called _protected_print

 

From the result, an attribute or method with a prefixed underscore acts the same as the normal one. So, why do we need to add a prefixed underscore for an attribute?

The prefix underscore is a warning for developers: please be careful about this attribute or method, don’t use it outside of the declared Class!

Private Attribute and Method

In traditional OOP languages, why can private attributes and methods not be accessed by a derived Class? Because it is useful in information hiding.

Suppose we declare an attribute with the name mood, but in the derived Class, we re-declare another attribute with the name mood. This overrides the previous one in the parent Class and will likely introduce a bug in the code.

So, how do we use the private attribute in Python?

The answer is adding a double prefix underscore in an attribute or method. Let’s run this code snippet:

class Base(object):

    def __private(self):

        print("private value in Base")

    def _protected(self):

        print("protected value in Base")

    def public(self):

        print("public value in Base")

        self.__private()

        self._protected()

class Derived(Base):

    def __private(self):

        print("derived private")

    def _protected(self):

        print("derived protected")

d = Derived()

d.public()

The output will be:

public value in Base
private value in Base
derived protected
 

We call the public function from a derived object, which will invoke the public function in the Base class.

Note: Because __private is a private method, only the object itself could use it, there is no naming conflict for a private method.

 

 

 

 

 

 


Comments