in

Encapsulation in Python- Day 14

In this article we will learn about encapsulation in python.

Encapsulation work as a lock for our code.

New here ? — Learn python from scratch

Start learning Python from scratch for Free.

Encapsulation in Python- Day 14
Encapsulation in Python- Day 14

Just the way a lock prevents others from accessing your property, we can restrict other parts of the code from directly accessing sensitive data.

Consider the below code where the customer has a wallet_balance and there are methods that allow us to access and update that balance based on some logic.

class Customer:
    def __init__(self, cust_id, name, age, wallet_balance):
        self.cust_id = cust_id
        self.name = name
        self.age = age
        self.wallet_balance = wallet_balance
    def update_balance(self,amount):
        if amount < 1000 and amount > 0:
            self.wallet_balance += amount
    def show_balance(self):
            print("The balance is ",self.wallet_balance)
c1=Customer(100, "Gopal", 24, 1000)
c1.update_balance(500)
c1.show_balance()

But with the way currently, it is coded, the data can be accidentally changed by directly assigning an incorrect value to it.

Encapsulation in Python
Encapsulation in Python

Adding a double underscore makes the attribute a private attribute. Private attributes are those which are accessible only inside the class.

This method of restricting access to our data is called encapsulation (Encapsulation in Python).

How does Encapsulation works?

When we put a double underscore in front of the attribute name, python will internally change its name to _Classname__attribute

This is why we get an error when we try to access a private attribute. 

Observe this code and execute in Eclipse IDE.

class Customer:
    def __init__(self, cust_id, name, age, wallet_balance):
        self.cust_id = cust_id
        self.name = name
        self.age = age
        self.__wallet_balance = wallet_balance
    def update_balance(self,amount):
        if amount < 1000 and amount > 0:
            self.__wallet_balance += amount
    def show_balance(self):
        print("The balance is ",self.__wallet_balance)
c1=Customer(100, "Gopal", 24, 1000)
print(c1.__wallet_balance)

We can access private variable using the modified name  as  _Classname__attribute

So, if private variable can be accessed outside the class and can be modified, then what is the advantage of making it private?

Thus in python encapsulation is more like a caution sign than a lock. A caution sign is there so that you don’t accidentally break a rule.

But if you still want to break it you can, with consequence.

Getter and Setter in python

To have an error-free way of accessing and updating private variables, we create specific methods for this.

Those methods which are meant to set a value to a private variable are called setter methods and methods meant to access private variable values are called getter methods.

The below code is an example of getter and setter methods:

class Customer:
    def __init__(self, id, name, age, wallet_balance):
        self.id = id
        self.name = name
        self.age = age
        self.__wallet_balance = wallet_balance
    def set_wallet_balance(self, amount):
        if amount < 1000 and amount>  0:
            self.__wallet_balance = amount
    def get_wallet_balance(self):
        return self.__wallet_balance
c1=Customer(100, "Gopal", 24, 1000)
c1.set_wallet_balance(120)
print(c1.get_wallet_balance())

Example on python encapsulation

End of encapsulation in python article;

Thank You.


What do you think?

Written by My Inquisitor

Comments

Leave a Reply

GIPHY App Key not set. Please check settings

Loading…

0

Ready to use contact form [Just Copy and Paste]

Famous White Hat Hackers