Unit III File Handling , Classes_Part7
Unit III File Handling , Classes_Part7
Encapsulation
Using Private Instance Variables
Inheritance
Accessing Inherited Variables & Methods
Use of super()
Polymorphism
Operator Overloading & Magic Methods
Encapsulation
In Python, encapsulation refers to the bundling of data
(attributes) and methods (functions) that operate on
the data into a single unit
Encapsulation features :
Organized variable and its methods.
Avoids unnecessary modification of variable.
Can use across the modular programming.
Example
class Public:
def __init__(self ):
self.name = "John" # Public attribute
def display_name(self):
print(self.name) # Public method
obj = Public()
obj.display_name() # Accessible
print(obj.name) # Accessible
Polymorphism
Polymorphism means that you can have multiple
classes where each class implements the same
variables or methods in different ways. Polymorphism
takes advantages of inheritance in order to make this
happen.
Example:
def add(a, b):
return a + b
print(add(3, 4)) # Integer addition
print(add("Hello, ", "World!")) # String concatenation
print(add([1, 2], [3, 4])) # List concatenation
Example 2:
class Shape: class Circle(Shape):
def area(self): def __init__(self, radius):
return "Undefined" self.radius = radius
def area(self):
class Rectangle(Shape): return 3.14 * self.radius ** 2
def __init__(self, length, width):
self.length = length shapes = [Rectangle(2, 3),
self.width = width Circle(5)]
def area(self): for shape in shapes:
return self.length * self.width print(f"Area: {shape.area()}")
Operator Overloading
Normally operators like +,-,/,*, works fine with built-in datatypes.
Changing the behavior of an operator so that it works with programmer
defined types(class) is called operator overloading.
Basic operators like +, -, * etc. can be overloaded. To overload an operator, one
needs to write a method within user-defined class. The method should consist
of the code what the programmer is willing to do with the operator.
Let us consider an example to overload + operator to add two Time
objects by defining add method inside the class.
In the above example,
when the statement t3=t1+t2 is used, it invokes a special method add ()
written inside the class. Because, internal meaning of this statement is t3 = t1.
add (t2)
Here, t1 is the object invoking the method. Hence, self inside add () is the
reference (alias) of t1. And, t2 is passed as argument explicitly.
Python provides a special set of methods which have to be used for overloading
operator. Following table shows gives a list of operators and their respective
Python methods for overloading.
Exception Handling
Using try… except… finally
The try block lets you test a block of code for errors.
The except block lets you handle the error.
The else block lets you execute code when there is no
error.
The finally block lets you execute code, regardless of the
result of the try- and except blocks.
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")