0% found this document useful (0 votes)
39 views

Unit 2

Here are the key points about protected members in Python: - Protected members in Python are achieved by prefixing the member name with a single underscore (_) - Protected members can be accessed from within the class and any subclasses - They can also be accessed directly from outside the class, but it is considered a convention not to do so - Protected members allow subclasses to access and modify members of the parent class, while keeping them private to outside code - The single underscore prefix is only a convention - there is no enforcement of protected access, but it indicates the member is meant for internal class/subclass use So in summary, protected members provide a way to share access between a class and its subclasses, while keeping

Uploaded by

Vipul Maheshwari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Unit 2

Here are the key points about protected members in Python: - Protected members in Python are achieved by prefixing the member name with a single underscore (_) - Protected members can be accessed from within the class and any subclasses - They can also be accessed directly from outside the class, but it is considered a convention not to do so - Protected members allow subclasses to access and modify members of the parent class, while keeping them private to outside code - The single underscore prefix is only a convention - there is no enforcement of protected access, but it indicates the member is meant for internal class/subclass use So in summary, protected members provide a way to share access between a class and its subclasses, while keeping

Uploaded by

Vipul Maheshwari
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 86

Object Oriented

Programming
OOP Features
Main Concepts of Object-Oriented Programming (OOPs)

• Class
• Objects
• Polymorphism
• Compile-Time (Function Overloading, Operator Overloading)
• Run-Time (Method Overriding)
• Encapsulation
• Inheritance
• Data Abstraction

2
OOP, Defining a Class
• Python was built as a procedural language
– OOP exists and works fine, but feels a bit more "tacked on"
– Java probably does classes better than Python (gasp)

• Declaring a class:
class name:
statements

3
Fields
name = value
– Example: point.py
class Point: 1 class Point:
x = 0 2 x = 0
y = 0 3 y = 0

# main
p1 = Point()
p1.x = 2
p1.y = -5

– can be declared directly inside class (as shown here)


or in constructors (more common)
– Python does not really have encapsulation or private fields
• relies on caller to "be nice" and not mess with objects' contents

4
Using a Class
import class
– client programs must import the classes they use

point_main.py
1 from Point import *
2
3 # main
4 p1 = Point()
5 p1.x = 7
6 p1.y = -3
7 ...
8
9 # Python objects are dynamic (can add fields any time!)
10 p1.name = "Tyler Durden"

5
Object Methods
def name(self, parameter, ..., parameter):
statements

– self must be the first parameter to any object method


• represents the "implicit parameter" (this in Java)

– must access the object's fields through the self reference


class Point:
def translate(self, dx, dy):
self.x += dx
self.y += dy
...

6
"Implicit" Parameter (self)
• Java: this, implicit
public void translate(int dx, int dy) {
x += dx; // this.x += dx;
y += dy; // this.y += dy;
}

• Python: self, explicit


def translate(self, dx, dy):
self.x += dx
self.y += dy

– Exercise: Write distance, set_location, and


distance_from_origin methods.

7
Exercise Answer
point.py
1 from math import *
2
3 class Point:
4 x = 0
5 y = 0
6
7 def set_location(self, x, y):
8 self.x = x
9 self.y = y
10
11 def distance_from_origin(self):
12 return sqrt(self.x * self.x + self.y * self.y)
13
14 def distance(self, other):
15 dx = self.x - other.x
16 dy = self.y - other.y
17 return sqrt(dx * dx + dy * dy)

8
Calling Methods
• A client can call the methods of an object in two ways:
– (the value of self can be an implicit or explicit parameter)

1) object.method(parameters)
or
2) Class.method(object, parameters)

• Example:
p = Point(3, -4)
p.translate(1, 5)
Point.translate(p, 1, 5)

9
Constructors
• Constructors are generally used for instantiating an object. The
task of constructors is to initialize(assign values) to the data
members of the class when an object of the class is created.

def __init__(self, parameter, ..., parameter):


statements
– a constructor is a special method with the name __init__
– Example:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
...

How would we make it possible to construct a


Point() with no parameters to get (0, 0)?
10
toString and __str__
def __str__(self):
return string
– equivalent to Java's toString (converts object to a string)
– invoked automatically when str or print is called

Exercise: Write a __str__ method for Point objects that


returns strings like "(3, -14)"
def __str__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"

11
Complete Point Class
point.py
1 from math import *
2
3 class Point:
4 def __init__(self, x, y):
5 self.x = x
6 self.y = y
7
8 def distance_from_origin(self):
9 return sqrt(self.x * self.x + self.y * self.y)
10
11 def distance(self, other):
12 dx = self.x - other.x
13 dy = self.y - other.y
14 return sqrt(dx * dx + dy * dy)
15
16 def translate(self, dx, dy):
17 self.x += dx
18 self.y += dy
19
20 def __str__(self):
21 return "(" + str(self.x) + ", " + str(self.y) + ")"

12
Python Class and Object Example1
Dog.py
1 class Dog:
2 # class attribute
3 attr1 = "mammal"
4 # Instance attribute
5 def __init__(self, name):
6 self.name = name
7
8 # Object instantiation
9 Rodger = Dog("Rodger")
10
11 Tommy = Dog("Tommy")
12
13 # Accessing class attributes
14 print("Rodger is a", Rodger.attr1)
15 print("Rodger is a", Tommy.attr1)
16
17
18 # Accessing instance attributes
19 print("My name is", Rodger.name)
20 print("My name is", Tommy.name)
21

13
Python Class and Object Example2
Dog1.py
1 class Dog1:
2 # class attribute
3
4 attr1 = "mammal“
5
6 # Instance attribute
7 def __init__(self, name):
8 self.name = name
9 def speak(self):
10
11 print("My name is {}".format(self.name))
12
13 # Object instantiation
14 Rodger = Dog1("Rodger")
15 Tommy = Dog1("Tommy")
16
17
18 # Accessing class methods
19 Rodger.speak()
20 Tommy.speak()
21

14
Encapsulation
• Encapsulation is the mechanism for restricting the access to some
of an objects's components, this means, that the internal
representation of an object can't be seen from outside of the
objects definition.
• C++, Java, and C# rely on the public, private, and protected
keywords in order to implement variable scoping and
encapsulation.
• In python, We don’t have public, protected or private keywords,
but we use underscore to restrict a variable for outside
accessibility.

15
Public, Protected and Private
• If an identifier doesn't start with an underscore character "_" it can
be accessed from outside, i.e. the value can be read and changed.
• Data can be protected by making members private or protected.
• Instance variable names starting with two underscore characters
cannot be accessed from outside of the class.
• At least not directly, but they can be accessed through private
name mangling.
• That means, private data __A can be accessed by the following
name construct: instance_name._classname__A

16
Public, Protected and Private
• If an identifier is only preceded by one underscore character, it is a
protected member.
• Protected members can be accessed like public members from
outside of class

Example:
class Encapsulation(object):
def __init__(self, a, b, c):
self.public = a
self._protected = b
self.__private = c

17
Public, Protected and Private
• The following interactive sessions shows the behavior of public, protected and
private members:

18
Public, Protected and Private
• The following table shows the different behavior Public, Protected
and Private Data:

19
Protected Members
• Protected members (in C++ and JAVA) are those members of the
class that cannot be accessed outside the class but can be accessed
from within the class and its subclasses.

• To accomplish this in Python, just follow the convention by


prefixing the name of the member by a single underscore “_”.

• Although the protected variable can be accessed out of the class as


well as in the derived class(modified too in derived class), it is
customary(convention not a rule) to not access the protected out
the class body.

20
Protected Members
Example : Python program to demonstrate protected members
# Creating a base class
class Base:
def __init__(self):

# Protected member
self._a = 2

# Creating a derived class


class Derived(Base):
def __init__(self):

# Calling constructor of Base class


Base.__init__(self)
print("Calling protected member of base class: ", self._a)

# Modify the protected variable:


self._a = 3
print("Calling modified protected member outside class: ", self._a)

21
Protected Members
Example cont...
obj1 = Derived()

obj2 = Base()

# Calling protected member


# Can be accessed but should not be done due to convention
print("Accessing protected member of obj1: ", obj1._a)

# Accessing the protected variable outside


print("Accessing protected member of obj2: ", obj2._a)

22
Private Members
• Private members are similar to protected members, the difference
is that the class members declared private should neither be
accessed outside the class nor by any derived class.

• In Python, there is no existence of Private instance variables that


cannot be accessed except inside a class.

• However, to define a private member prefix the member name


with double underscore “__”.

• Python’s private members can be accessed outside the class


through python name mangling.

23
Private Members
Example : Python program to demonstrate private members

# Creating a base class


class Base:
def __init__(self):

# Public member
self.a = “Welcome to Python”
# Private member
self.__c = “Welcome to Python”

# Creating a derived class


class Derived(Base):
def __init__(self):

# Calling constructor of Base class


Base.__init__(self)
print("Calling private member of base class: ")
print(self.__c)

24
Private Members
Example cont... # Driver code
obj1 = Base()
print(obj1.a) # Works fine
print(obj1.__c) # will raise an AttributeError

obj2 = Derived()
# will also raise an AtrributeError as private member of
base class is called inside derived class

25
Naming Using Underscore(_)
• Underscore(_) can be used to name variables, functions and
classes, etc.
 Single Pre Underscore:- _variable
 Single Post Underscore:- variable_
 Double Pre Underscores:- __variable
 Double Pre and Post Underscores:- __variable__
1. _single_pre_underscore (_name)
Single Pre Underscore is used for internal use. Most of us don't use it
because of that reason.
• single pre underscore doesn't
class Test: stop you from accessing
def __init__(self): the single pre
self.name = "datacamp"
self._num = 7 obj = Test()
underscore variable.
print(obj.name) print(obj._num) • But, single pre
obj = Test() underscore effects the names
print(obj.name) that are imported from the
print(obj._num) module. 26
Naming Using Underscore(_)
_single_pre_underscore (_name) cont...
## filename:- my_functions.py
Create the following python file def func():
return “Welcome to python"
def _private_func():
return 7

Now, if you import all the methods and names


from my_functions.py, Python doesn't import the names which starts with
a single pre underscore.
To avoid this error, import module
normally.

27
Naming Using Underscore(_)
2. single_post_underscore (name_)
– Sometimes if you want to use Python Keywords as a variable, function
or class names, you can use this convention for that.
– You can avoid conflicts with the Python Keywords by adding
an underscore at the end of the name which you want to use.

Ex:

Single Post Underscore is used for


naming your variables as Python
Keywords and to avoid the clashes
by adding an underscore at last of
your variable name.

28
Naming Using Underscore(_)
3. Double Pre Underscore (__name)
– Double Pre Underscores are used for the name mangling.
– Name Mangling:- interpreter of the Python alters the variable name in a
way that it is challenging to clash when the class is inherited.

This code returns all the attributes of


Ex:
the class object.
• self.a variable appears in the list without
any change.
• self._b Variable also appears in the list
without any change.
• Is there self.__c variable in the list?
• If you carefully look at the attributes
list, you will find an attribute
called _Sample__c. This is the name
mangling.

29
Naming Using Underscore(_)
3. Double Pre Underscore cont...
– Let's create another class by inheriting Sample class to see how
overriding works.
Ex:

30
Naming Using Underscore(_)
3. Double Pre Underscore cont...
– Can access the Double Pre Underscore variables using methods in the
class
Ex:

31
Naming Using Underscore(_)
3. Double Pre Underscore cont...
– Can also use the Double Pre Underscore for the method names.
Ex:

32
Naming Using Underscore(_)
4. Double Pre And Post Underscore (__name__)
– In Python, you will find different names which start and end with
the double underscore. They are called as magic methods or dunder
methods.
Ex:

33
Inheritance
• Inheritance is the capability of one class to derive or inherit the
properties from another class.
Python Inheritance Syntax

– Example:
class Point:
x = 0
y = 0
class Point3D(Point): # Point3D extends Point
z = 0
...

34
Calling Superclass Methods
• methods: class.method(object,
parameters)
• constructors: class.__init__(parameters)

class Point3D(Point):
z = 0
def __init__(self, x, y, z):
Point.__init__(self, x, y)
self.z = z

def translate(self, dx, dy, dz):


Point.translate(self, dx, dy)
self.z += dz

35
Inheritance Example
class Person: # Parent class
# Constructor
def __init__(self, name):
self.name = name

# To get name # An Object of Person


def getName(self): emp = Person(“John”)
return self.name print(emp.getName())
print(emp.isEmployee())
# To check if this person is an employee
def isEmployee(self): # An Object of Employee
return False emp = Employee(“Ram”)
print(emp.getName())
# Inherited or Subclass print(emp.isEmployee())
class Employee(Person):

# Here we return true


def isEmployee(self):
return True

36
Subclassing (Calling
constructor of parent class)
• A child class needs to identify which class is its parent class. This can be done by
mentioning the parent class name in the definition of the child class.
Eg: class subclass_name(superclass_name):
# Python code to demonstrate how parent constructors are called.
# parent class
class Person:
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber # child class
def display(self): class Employee(Person):
print(self.name) def __init__(self, name, idnumber, salary, post):
print(self.idnumber) self.salary = salary
self.post = post

# invoking the constructor of parent class


Person.__init__(self, name, idnumber)

# creation of an object variable or an instance


a = Employee('Rahul', 886012, 200000, "Intern")
# calling a function of the class Person using its instance
a.display()
37
Subclassing (Calling
constructor of parent class)
Python program to demonstrate error if we forget to invoke __init__() of the
parent
• If you forget to invoke the __init__() of the parent class then its instance variables
would not be available to the child class.
class A:
def __init__(self, n='Rahul'):
self.name = n

class B(A):
def __init__(self, roll):
self.roll = roll

object = B(23)
print(object.roll) # No Error
print(object.name) # Error

38
Types of Inheritance in Python
 Single Inheritance: Single inheritance enables a derived class to inherit
properties from a single parent class, thus enabling code reusability and the
addition of new features to existing code.
# Python program to demonstrate single inheritance
# Base class
class Parent:
def func1(self):
print("This function is in parent class.")

# Derived class
class Child(Parent):
def func2(self):
print("This function is in child class.")

# Driver's code
object = Child() Output:
object.func1()
object.func2()

39
Types of Inheritance in Python
 Multiple Inheritance: When a class can be derived from more than one base
class this type of inheritance is called multiple inheritances. (Unlike java,
python shows multiple inheritance.)
# Python program to demonstrate multiple inheritance
# Base class1
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
# Driver's code
class Father:
s1 = Son()
fathername = ""
s1.fathername = "RAM"
def father(self):
s1.mothername = "SITA"
print(self.fathername)
s1.parents()
# Derived class
class Son(Mother, Father):
def parents(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)

40
Types of Inheritance in Python
 Multiple Inheritance: When the method is overridden in both classes
Method Resolution Order (MRO) will take place from left to right.
class Class1: class Class1:
def m(self): def m(self):
print("In Class1") print("In Class1")

class Class2: class Class2:


def m(self): def m(self):
print("In Class2") print("In Class2")

class Class3(Class1, Class2): class Class3(Class2, Class1):


pass pass

obj = Class3() obj = Class3()


obj.m() obj.m()
Output: In Class1 Output: In Class2

NOTE: In the case of multiple inheritance, a given attribute is


first searched in the current class if it’s not found then it’s
searched in the parent classes. The parent classes are searched in
a left-right fashion and each class is searched once. 41
Types of Inheritance in Python
 Multilevel Inheritance: In multilevel inheritance, features of the base class and
the derived class are further inherited into the new derived class. This is like a
relationship representing a child and a grandfather.

42
Types of Inheritance in Python
# Python program to demonstrate multilevel inheritance

# Base class
class Grandfather:
def __init__(self, grandfathername):
self.grandfathername = grandfathername
# Intermediate class
class Father(Grandfather):
def __init__(self, fathername, grandfathername):
self.fathername = fathername
# invoking constructor of Grandfather class # Driver code
Grandfather.__init__(self, grandfathername) s1 = Son('Prince', 'Rampal',
# Derived class 'Lal mani')
class Son(Father): print(s1.grandfathername)
def __init__(self, sonname, fathername, s1.print_name()
grandfathername):
self.sonname = sonname
# invoking constructor of Father class
Father.__init__(self, fathername, grandfathername)
def print_name(self):
print('Grandfather name :', self.grandfathername)
print("Father name :", self.fathername)
print("Son name :", self.sonname)
43
Types of Inheritance in Python
 Hierarchical Inheritance: When more than one derived class are created from
a single base class, this type of inheritance is called hierarchical inheritance.
# Python program to demonstrate Hierarchical inheritance

# Base class
class Parent:
def func1(self):
print("This function is in parent class.")
# Derived class1
class Child1(Parent): # Driver's code
def func2(self): object1 = Child1()
print("This function is in child 1.") object2 = Child2()
# Derived class2 object1.func1()
class Child2(Parent): object1.func2()
def func3(self): object2.func1()
print("This function is in child 2.") object2.func3()

44
Types of Inheritance in Python
 Hybrid Inheritance: Inheritance consisting of multiple types of inheritance is
called hybrid inheritance.

Hybrid
Inheritance

Multiple
Inheritance

45
Types of Inheritance in Python
# Python program to demonstrate Hybrid inheritance

46
Function Overloading
 Like other languages do, python does not support method overloading by
default. But there are different ways to achieve method overloading in Python.
 The problem with method overloading in Python is that we may overload the
methods but can only use the latest defined method.

 Example1: Method1 (2 Args) Method2 (3 Args)


def product(a, b): def product(a, b,c):
p = a * b p = a * b * c
print(p) print(p)
>> Product(4,5) # Error >> Product(2,3,4) # No error

 Example 2:

47
How to Achieve Function
Overloading in Python
 Method1 :
If we want to implement the concept of function overloading, we need to set
the default values of the parameters of the function as None. By setting the
value of functional parameters as None, we get the option of calling the
function either with or without the parameter.
 Method2:
By Using Multiple Dispatch Decorator
Multiple Dispatch Decorator Can be installed by:

pip3 install multipledispatch

48
How to Achieve Function
Overloading in Python
Using Method 1: Calculate the area of figures(triangle, rectangle, square).

49
How to Achieve Function
Overloading in Python
Using Method 2: Using Multiple Dispatch Decorator
from multipledispatch import dispatch

#passing two parameter


@dispatch(int,int)
def product(first,second):
result = first*second
print(result);
#passing three parameters
@dispatch(int,int,int)
def product(first,second,third):
result = first * second * third
print(result);
#you can also pass data type of any value as per requirement
@dispatch(float,float,float)
def product(first,second,third):
result = first * second * third
print(result);

>> product(2,3)
>> Product(2,3,4)
>> product(2.2,3.4,2.3)
50
Method Overriding
 Method overriding is an ability of any object-oriented programming language
that allows a subclass or child class to provide a specific implementation of a
method that is already provided by one of its super-classes or parent classes.

 When a method in a subclass has the same name, same parameters or signature
and same return type(or sub-type) as a method in its super-class, then the
method in the subclass is said to override the method in the super-class.

51
Method Overriding
 Method Overriding Example:
# Defining parent class
>> class Parent():
def __init__(self):
self.value = "Inside Parent"
# Parent's show method
def show(self):
print(self.value)
# Defining child class
>> class Child(Parent):
def __init__(self):
self.value = "Inside Child"
# Child's show method
def show(self):
print(self.value)

>> obj1 = Parent()


>> obj2 = Child()

>> obj1.show()
>> obj2.show()
52
Method Overriding with Multiple
Inheritance
 Method Overriding with Multiple Inheritance Example:
>> class Parent1():
# Parent1's show method
def show(self):
print("Inside Parent1")

>> class Parent2():


# Parent2's show method
def display(self):
print("Inside Parent2")

>> class Child(Parent1, Parent2):


# Child's show method
def show(self):
print("Inside Child")
obj = Child()

>> obj.show()
>> obj.display()

53
Method Overriding with
Multilevel Inheritance
 Method Overriding with Multilevel Inheritance Example:
>> class Parent():
# Parent's show method
def display(self):
print("Inside Parent")

>> class Child(Parent):


# Child's show method
def show(self):
print("Inside Child")

>> class GrandChild(Child):


# Child's show method
def show(self):
print("Inside GrandChild")

>> g = GrandChild()
>> g.show()
>> g.display()

54
Calling the Parent’s method
within the overridden method
 Parent class methods can also be called within the overridden methods. This can
generally be achieved by two ways.
1. Using Classname: 2. Using Super()
class Parent():
class Parent():
def show(self):
print("Inside Parent") def show(self):
print("Inside Parent")
class Child(Parent):
class Child(Parent):
def show(self):
def show(self):
# Calling the parent's class
# method # Calling the parent's class
Parent.show(self) # method
print("Inside Child") super().show()
print("Inside Child")
# Driver's code
# Driver's code
obj = Child() obj = Child()
obj.show() obj.show()
55
Operator Overloading
 Operator Overloading means giving extended meaning beyond their predefined
operational meaning. For example operator + is used to add two integers as well
as join two strings and merge two lists.

# Python program to show use of + operator


for different purposes.

>> print(1 + 2)

# concatenate two strings


>> print("Geeks"+"For")

# Product two numbers


>> print(3 * 4)

# Repeat the String


>> print("Geeks"*4)

56
How to Overload an Operator
 To perform operator overloading, Python provides some special function or
magic function that is automatically invoked when it is associated with that
particular operator.

 For example, when we use + operator, the magic method __add__ is


automatically invoked in which the operation for + operator is defined.

Overloading binary + operator in Python :

• When we use + operator, the magic method __add__ is automatically invoked in


which the operation for + operator is defined.

• There by changing this magic method’s code, we can give extra meaning to the
+ operator.

57
How to Overload an Operator
 Program to overload binary + operator

class A:
def __init__(self, a):
self.a = a

# adding two objects


def __add__(self, o):
return self.a + o.a
ob1 = A(1)
ob2 = A(2)
ob3 = A("Geeks")
ob4 = A("For")

print(ob1 + ob2)
print(ob3 + ob4)

58
How to Overload an Operator
 Program to overload comparison operators

Checking which object is greater

class A:
def __init__(self, a):
self.a = a
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
print("ob1 is greater than ob2")
else:
print("ob2 is greater than ob1")

59
How to Overload an Operator
 Overloading equality and less than operators :

>> class A:
def __init__(self, a):
self.a = a
def __lt__(self, other):
if(self.a<other.a):
return "ob1 is lessthan ob2"
else:
return "ob2 is less than ob1"
def __eq__(self, other):
if(self.a == other.a):
return "Both are equal"
else:
return "Not equal"
>> ob1 = A(2)
>> ob2 = A(3)
>> print(ob1 < ob2)

>> ob3 = A(4)


>> ob4 = A(4)
>> print(ob1 == ob2)
60
Exception Handling

61
Exceptions

What is an exception?

Even if a statement or
expression is syntactically correct, it
may cause an error when an attempt
is made to execute it. Errors
detected during execution are
called exceptions

62
Exceptions

For Example

63
Handling Exceptions
It is possible to write programs
that handle selected exceptions.
Look at the following example,
which asks the user for input until a
valid integer has been entered, but
allows the user to interrupt the
program (using Control-C or
whatever the operating system
supports); note that a user-
generated interruption is signalled
by raising the KeyboardInterrupt 64
Handling Exceptions

For Example

65
Handling Exceptions

The try statement works as follows.

First, the try clause (the statement(s)


between the try and except
keywords) is executed.

If no exception occurs, the except


clause is skipped and execution of
the try statement is finished.

66
Handling Exceptions

The try statement works as follows.

If an exception occurs during


execution of the try clause, the rest
of the clause is skipped. Then if its
type matches the exception named
after the except keyword, the except
clause is executed, and then
execution continues after the try
statement.
67
Handling Exceptions

The try statement works as follows.


If an exception occurs which does
not match the exception named in
the except clause, it is passed on to
outer try statements; if no handler is
found, it is an unhandled exception
and execution stops with a message
as shown above.

68
The except Clause with No Exceptions

69
The except Clause with No Exceptions

You can also use the except statement


with no exceptions defined as follows:
try:
You do your operations here;
except:
If there is any exception, then
execute this block…..
else:
If there is no exception then execute
this block. Contd..
70
The except Clause with No Exceptions

This kind of a try-


except statement catches all the
exceptions that occur. Using this
kind of try-except statement is not
considered a good programming
practice though, because it catches
all exceptions but does not make the
programmer identify the root cause
of the problem that may occur.

71
Examples

72
Example

73
Example

74
Catching Specific Exception

A try statement can have more than


one except clause, to specify handlers for
different exceptions. Please note that at
most one handler will be executed.

try:
# statement(s)
except IndexError:
# statement(s)
except ValueError:
# statement(s)

75
Example : Multiple Exception Handling

76
Example : Multiple Exception Handling

77
Try with Else Clause
In python, you can also use the else clause
on the try-except block which must be present
after all the except clauses. The code enters the
else block only if the try clause does not raise an
exception.

78
raise statement

79
raise statement

The raise statement allows the


programmer to force a specific exception to
occur. The sole argument in raise indicates
the exception to be raised.

Raising an exception breaks current


code execution and returns the exception
back until it is handled.
Syntax:

raise [expression1[, expression2]]


80
raise Example 1

81
raise Example 2

82
Some common exceptions

83
Some common exceptions

IOError
If the file cannot be opened.

ImportError
If python cannot find the module.

ValueError
Raised when a built-in operation or
function receives an argument that has the
right type but an inappropriate value.

84
Some common exceptions

KeyboardInterrupt
Raised when the user hits the interrupt key
(normally Control-C or Delete).

EOFError
Raised when one of the built-in functions
(input() or raw_input()) hits an end-of-file
condition (EOF) without reading any data

85
Thank You

86

You might also like