Slides For Python
Slides For Python
in Python
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 1/31
Outline
3 Inheritance
4 Polymorphism
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 2/31
Acknowledgement and disclaimer
All mistakes (if any) are ours.
We have used several other sources, which we have referred to in the
appropriate places.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 3/31
Section 1
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 4/31
What is this?
l = list('Python')
Ultimately the list class hides away the details of what is going in the
background for the object l.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 5/31
This is a very powerful concept. The object l can now invoke several
methods that are defined in the list class.
However, the user doesn’t need to know the details of how these
methods are implemented. The user just needs to know how to call a
particular method he/she wants to use.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 6/31
Section 2
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 7/31
An example of classes and objects around us
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 8/31
Just to code this up:
class office():
def __init__(shellfish, room_num, occ, name_occupant, wb, pb):
shellfish.room_number = room_num
shellfish.occupied = occ
shellfish.person = None
if shellfish.occupied:
shellfish.person = name_occupant
shellfish.whiteboard_installed = wb
shellfish.pinboard_installed = pb
def print_office_info(shellfish):
print('Room Number: ', shellfish.room_number,
'Occupation status: ', shellfish.occupied,
'Occupied by: ', shellfish.person,
'Whiteboard Installed:', shellfish.whiteboard_installed,
'Pinboard Installed:', shellfish.pinboard_installed)
Let’s say that besides offices, we consider lecture halls and hostel
rooms.
Can we say that we have a super class called room and subclasses like:
office, lecture hall, hostel room?
The subclasses can have features that are unique to each one of them,
but have some common attributes from the super class.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 10/31
Examples of Inheritance around us.
Cars?
What are some other examples that you can think about?
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 11/31
Section 3
Inheritance
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 12/31
Inheritance
Inheritance is the capability of one class to derive or inherit the
properties (data + functions) from another class.
class Parent():
def show(self):
print("Inside Parent class")
class Child(Parent):
def display(self):
print("Inside Child class")
obj = Child()
obj.display()
Next, let’s try to code up our earlier room class example, using
inheritance.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 14/31
Our room example
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 15/31
Another Example (Points & Rectangles)
Let us look at another example of class inheritance.
We specify one corner (lower left) of the rectangle, its width, and its
height.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 16/31
Multiple inheritance
This allows the child class to get access to member variables and
member functions of multiple parent classes.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 18/31
Section 4
Polymorphism
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 19/31
What does the word polymorphism mean?
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 20/31
Polymorphism in Python
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 21/31
Example: Method Overriding
class Parent():
def show(self):
print("Inside Parent class")
class Child(Parent):
def show(self):
print("Inside Child class")
obj1 = Child()
obj1.show()
obj2 = Parent()
obj2.show()
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 22/31
Example: Method Overriding
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 23/31
Section 5
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 24/31
Queue
1
Figure: Analogous to people lining up to wait for goods or services.
class Queue:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def insert_(self, item): #enqueue
self.items.insert(0,item)
def remove(self): #dequeue
return self.items.pop()
def size(self):
return len(self.items)
def peek(self): #return element at the head
return self.items[len(self.items)-1]
List functions:
https://docs.python.org/3/tutorial/datastructures.html
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 26/31
Stack
2
Figure: Analogous to a set of physical items stacked on top of each other.
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 27/31
Implementing stacks using Python
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def insert_(self, item): #push
self.items.append(item)
def remove(self): #pop
return self.items.pop()
def size(self):
return len(self.items)
def peek(self): #return element at the top
return self.items[len(self.items)-1]
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 28/31
Do you notice something?
There was a lot of code that was repeated in the Stack class.
Can we share code between Stack and Queue using inheritance? Show
example (example_stack_inheritance_7.1.py)
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 29/31
What did we learn today?
3 Inheritance
4 Polymorphism
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 30/31
Thank you!
Sukrit Gupta and Nitin Auluck Intro to Computing and Data Structures 31/31