Complete Overview Of Encapsulation in Python

Complete Overview Of Encapsulation in Python

Where Encapsulation is helpful in Programing?

Encapsulation is used to achieve two things. and we achieve encapsulation approach using Class and Object.

Data Hiding : Hiding un necessary data from outside, reaveal only those which are required for processing. we hide that data in a class, and this data only be accesed using it related functions. it provide security , no one can manupulate our data directly.

Example: you can not modify your bank balance directly, in order to modify your bank balance, you have to go through one process of crediting some amount in acount or withdrawing some amount form your account. then only your balance will be modified. Means your Account Balance is hidden and protected.

Data Binding with its related methods: Se make sure that certain data is only accessible by its related methods.

Example: Student's data should be only accesible for student related task, it should not be accessible by employee's task,. so student's class data should be accessible only by student's method only.

Class is data type that we create on basis of our requirements in program. On the basis of this class we create object, in this object we have all variable and methods of the class.

Two types of variables is created in class, instance variable- that variable which is bind with self, is called instance variable, to access this we must need object to create.

Instance variable can be defined and assigned outside or inside of the class, There is 5 ways to do, check below methods.

Method 1:

# Method 1:
class A:
    pass

obj1=A()
obj1.x=100
print(obj1.__dict__)

Method 2:

# Method 2:
class B:
    def m1(self):
        self.x=100

objb=B()
objb.m1()
print(objb.__dict__)

Method 3:

# Method 3
class C:
    def init(self):
        self.x=100

objc=C()
print(objc.__dict__)

Method 4:

# Method 4:
class D:
    pass

objd=D()
objd.__dict__={'x':100}
print(objd.__dict__)

Method 5:

# Method 5
class E:
    pass

obje=E()
setattr(obje,"x",100)
print(obje.__dict__)

Output
{'x': 100} #method 1
{'x': 100} #method 2
{'x': 100} #method 3
{'x': 100} #method 4
{'x': 100} #method 5

Class level variable, when any varaibel is inside the class but outside of all the methods, it class level variable. This variable can be accessed using Classname.Varaibalename from inside class as well as outside of the class. Class level variable can be accessed by all the methods of that class. It is common for all the class. So thsose varible which supposed to command for all object we declare it as class variable.

There is 3 types of methods can be defined in class, 1st. instance method, those mehod of the class whose fiest argument is ‘self’ , called instane method. To call this mehod outside of the class, object is required. Objectname.mehodname() , instance method can acces instance variable as well as class variable of that class.

2nd, Class method, class method is that method inside the class whose first argument is ‘cls’ and above that method one decorator @classmethod should be attached. This decorator converts normal mehod into class method. This cls holds the reference of the current class, so any wherer inside the class we can call this method using ‘cls.classMethodName()’ or ‘className.classmethodName’ both is same.

Outside the class, class method can be directly using class name or by its object , <className.classMethodName()> or <objectName.classMethodName()>

3rd, static method, this method inside the class have decorator @staticmethod, this method doesn’t hold reference of object (self), or reference of class (cls), so it cant access instance variable or class variable. It has take only required argument for processing certain logic, and that retruns the result or print the result, whatever is required, process is done.

Inside the class, static method can be called with class name.

<className.staticMehtodname()> and outside the class static method can be called using ClassName.staticMethodname as well as objectName.staticMethodname

class Math:
    @staticmethod
    def power(num, p):
        return num**p

Math.power(4,5)
m1 = Math()
print(m1.func1(4, 5)

output:
1024
1024