[go: up one dir, main page]

0% found this document useful (0 votes)
8 views9 pages

Unit III File Handling , Classes_Part7

The document covers key concepts of object-oriented programming in Python, including class attributes, encapsulation, inheritance, polymorphism, operator overloading, and exception handling. It explains how encapsulation organizes data and methods, polymorphism allows multiple classes to implement the same methods differently, and operator overloading enables custom behavior for operators. Additionally, it discusses exception handling using try-except blocks and the use of the raise keyword to throw exceptions.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Unit III File Handling , Classes_Part7

The document covers key concepts of object-oriented programming in Python, including class attributes, encapsulation, inheritance, polymorphism, operator overloading, and exception handling. It explains how encapsulation organizes data and methods, polymorphism allows multiple classes to implement the same methods differently, and operator overloading enables custom behavior for operators. Additionally, it discusses exception handling using try-except blocks and the use of the raise keyword to throw exceptions.

Uploaded by

Saraswathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

 Class Attributes & Data Attributes

 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.

 When an error occurs, or exception as we call it, Python


will normally stop and generate an error message. These
exceptions can be handled using the try statement:
Raise an exception
 As a Python developer you can choose to throw an
exception if a condition occurs.
 raise keyword : is used to throw an exception

x = -1

if x < 0:
raise Exception("Sorry, no numbers below zero")

You might also like