OOPS Python
OOPS Python
OOPS - Python
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECT ORIENTED PROGRAMMING
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OOP
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OOP
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
WHY OOPS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
WHY OOPS
• OOPs makes complex code easier to develop, more reliable, more maintainable,
and generally better.
• OOPs lets the coder to think about what should be exposed to the outside world
and what should be hidden.
• OOPs lets the coder to change the implementation of an object without affecting
any other code. (Encapsulation)
• OOPs allows the coder to have many different functions, all with the same
name, all doing the same job, but on different data. (Polymorphism)
• OOPs lets the coder to write a set of functions, then expand them in different
direction without changing or copying them in any way. (Inheritance)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECTS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECTS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ABSTRACTION
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ENCAPSULATION
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
POLYMORPHISM
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASSES IN PYTHON
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASSES IN PYTHON
class MyClass:
# member variables
variable1 = something
variable2 = something
# member functions
def function1(self, parameter1, ...):
self.variable1 = something else
# defining a new variable
self.variable3 = something
function1 statements...
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASS EXAMPLE
class MyClass:
"This is my second class"
a = 10
def func(self):
print('Hello‘)
# Output: 10
print(MyClass.a)
# Output: <function MyClass.func at 0x0000000003079BF8>
print(MyClass.func)
# Output: 'This is my second class'
print(MyClass.__doc__)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SELF
The self
• Class methods must have an extra first parameter in method
definition. We do not give a value for this parameter when we call
the method, Python provides it
• If we have a method which takes no arguments, then we still have to
have one argument – the self. See fun() in above simple example.
• This is similar to this pointer in C++ and this reference in Java.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Creating object for a class
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SAMPLE EXAMPLE
#Simple example
class Test:
# A sample method
def fun(self):
print("Hello")
# Driver code
obj = Test()
obj.fun()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CONSTRUCTORS
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Init METHOD
class Person:
# init method or constructor
def __init__(self, name):
self.name = name Output:
Hello, my name is RENU
def say_hi(self):
print('Hello, my name is', self.name)
p = Person(‘RENU')
p.say_hi()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CONSTRUCTORS
Types of constructors :
• default constructor :
The default constructor is simple constructor which doesn’t accept any
arguments. It’s definition has only one argument which is a reference to
the instance being constructed.
• parameterized constructor :
constructor with parameters is known as parameterized constructor.
The parameterized constructor take its first argument as a reference to
the instance being constructed known as self and the rest of the
arguments are provided by the programmer.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
DEAFUALT CONSTRUCTOR
class hello:
abc = ""
# default constructor
def __init__(self):
self.abc = “Hello"
# a method for printing data members
def print_abc(self):
print(self.abc)
# creating object of the class
obj = hello()
# calling the instance method using the object obj OUTPUT:
obj.print_abc()
Hello
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
PARAMETERIZED CONSTRUCTOR
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
PARAMETERIZED CONSTRUCTOR
Output :
First number = 1000
Second number = 2000
Addition of two numbers = 3000
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ENCAPSULATION
• Python does not have the private keyword, unlike some other object
oriented languages, but encapsulation can be done.
• Instead, it relies on the convention: a class variable that should not
directly be accessed should be prefixed with an underscore.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ENCAPSULATION
class Robot(object):
def __init__(self):
self.a = 123 OUTPUT
self._b = 123 123
123
self.__c = 123 Traceback (most recent call last):
File "test.py", line 10, in <module>
obj = Robot()
print(obj.__c)
print(obj.a) #public AttributeError: 'Robot' object has no attribute '__c'
print(obj._b) #protected
print(obj.__c) #private
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ENCAPSULATION
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Getters and setters
Private variables are intended to be changed using getter and setter methods. These
provide
indirect access to them:
class Robot(object):
def __init__(self):
self.__version = 22
def getVersion(self):
print(self.__version) OUTPUT:
def setVersion(self, version): 22
23
self.__version = version
obj = Robot()
obj.getVersion()
obj.setVersion(23)
obj.getVersion()
print(obj.__version)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
POLYMORPHISM EXAMPLE
class Bear(object):
def sound(self):
print("Groarrr")
class Dog(object):
def sound(self):
print("Woof woof!")
def makeSound(animalType):
animalType.sound()
bearObj = Bear()
dogObj = Dog() Output:
makeSound(bearObj)
Groarrr
Woof woof!
makeSound(dogObj)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Polymorphism with abstract class
class Document:
def __init__(self, name):
self.name = name
def show(self):
raise NotImplementedError("Subclass must implement abstract method")
class Pdf(Document):
def show(self):
return 'Show pdf contents!‘ Output:
class Word(Document): Document1: Show pdf contents!
Document2: Show pdf contents!
def show(self): Document3: Show word contents!
return 'Show word contents!‘
documents = [Pdf('Document1'),Pdf('Document2'),Word('Document3')]
for document in documents:
print document.name + ': ' + document.show()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Object Methods
Objects can also contain methods. Methods in objects are functions that
belongs to the object.
Let us create a method in the Person class:
Example
Insert a function that prints a greeting, and execute it on the p1 object:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age o/p:
def myfunc(self): Hello my name
print("Hello my name is " + self.name) is John
p1 = Person("John", 36)
p1.myfunc()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Modify Object Properties
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36) o/p:
40
p1.age = 40
print(p1.age)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Delete Object Properties
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Delete Objects
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Object References
>>>m=n
Initially n=300 – creates an object for integer classs
a new symbolic name or reference, to the same
object n
>>>m=400
>>>n=“foo”
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Object identity
>>> n = 300
>>> m = n • After the assignment m = n, m and n both point
>>> id(n) to the same object, confirmed by the fact that
60127840
>>> id(m) id(m) and id(n) return the same number.
60127840 • Once m is reassigned to 400, m and n point to
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Pass by Value
class Vehicle:
def __init__(self, name, kind , color , value):
self.name = name
self.kind = kind
self.color =color
self.value = value
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color,
self.kind, self.value)
return desc_str Output :
Fer is a red car worth $60000.00.
car1=Vehicle("Fer","car","red",60000.00) 140329908463544
car2=Vehicle("Jump","car","blue",10000.00)
Jump is a blue car worth $10000.00.
print(car1.description(), “\n”, id(car1)) 140329908463600
print(car2.description(), “\n”, id(car2)) car1 id : 140329908463600
car2 id : 140329908463600
car1=car2
print("car1 id : ", id(car1), "car2 id :", id(car2))
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Pass by Refernce
class Vehicle:
def __init__(self, name, kind , color , value):
self.name = name
self.kind = kind
self.color =color
self.value = value
def description(self):
desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color,
self.kind, self.value)
return desc_str
def change_value(car1_obj) :
print ("Id of object inside function", id(car1))
car1_obj.value=55000.00
car1=Vehicle("Fer","car","red",60000.00) Output :
car1.change_value() Id of object inside function
print(car1.description(), "\ncar1 : ",id(car1)) 140107920909984
Fer is a red car worth $55000.00.
car1 : 140107920909984
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Collection of Objects
class Mobile:
def __init__(self, brand, price):
self.brand = brand
self.price = price
mob1=Mobile("Apple", 1000)
mob2=Mobile("Samsung", 2000)
mob3=Mobile("Apple", 3000)
mob4=Mobile("Samsung", 4000)
mob5=Mobile("Apple", 5000)
mob3.brand=“Samsung” Output :
for mobile in list_of_mobiles: Apple 1000
print (mobile.brand,mobile.price) Samsung 2000
Samsung 3000
Samsung 4000
Apple 5000
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SESSION 2
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Instance Variables
• All classes create objects, and all objects contain characteristics called attributes (referred
to as properties in the opening paragraph).
• Use the __init__() method to initialize (e.g., specify) an object’s initial attributes by giving
them their default value (or state).
• This method must have at least one argument as well as the self variable, which refers to
the object itself (e.g., Dog).
Example :
class Dog: # Initializer / Instance Attributes
def __init__(self, name, age):
self.name = name
self.age = age
NOTE :
You will never have to call the __init__() method; it gets called automatically when
you create a new ‘Dog’ instance.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Need for Static
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static Variables
• All variables which are assigned a value in class declaration are class or static
variables.
• And variables which are assigned values inside class methods/constructor are
instance variables.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static Variables - Example
Example :
# Class for Computer Science Student
class CSStudent:
stream = 'cse‘
# Class Variable
def __init__(self,name,roll): # Constructor
self.name = name
# Instance Variable
self.roll = roll
# Instance Variable
# Objects of CSStudent class
Output :
a = CSStudent(‘John', 1)
cse
b = CSStudent(‘Peter', 2)
cse
print(a.stream) # prints "cse" John
print(b.stream) # prints "cse" Peter
print(a.name) # prints “John" 1
print(b.name) # prints “Peter" 2
print(a.roll) # prints "1" cse
print(b.roll) # prints "2"
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static Variables - Example
class Mobile:
discount = 50
def __init__(self, price, brand):
self.price = price
self.brand = brand
def purchase(self):
total = self.price - self.price * Mobile.discount / 100
print (self.brand, "mobile with price", self.price, "is available after discount at", total)
def enable_discount():
Mobile.discount = 50
def disable_discount():
Mobile.discount = 0
mob1=Mobile(20000, "Apple")
mob2=Mobile(30000, "Apple")
mob3=Mobile(5000, "Samsung")
enable_discount() Output :
mob1.purchase()
mob2.purchase() Apple mobile with price 20000 is available after discount at 10000.0
Apple mobile with price 30000 is available after discount at 15000.0
disable_discount()
mob3.purchase() Samsung mobile with price 5000 is available after discount at 5000.0
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static Variable - Encapsulation
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static variable - Encapsulation
class Mobile:
__discount = 50
def __init__(self, price, brand):
self.price = price
self.brand = brand
def purchase(self):
total = self.price - self.price * Mobile.__discount / 100
print ("Total is ",total)
@staticmethod
def get_discount(): Output :
50
return Mobile.__discount
@staticmethod
def set_discount(discount):
Mobile.__discount = discount
print (Mobile.get_discount())
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Static Method
• A method to access static variable without an object is needed.
• This is done by creating static methods.
• Static methods are those methods which can be accessed without an object.
They are accessed using the class name.
There are two rules in creating such static methods:
– The methods should not have self
– @staticmethod must be written on top of it
• A static method is also a method which is bound to the class and not the object
of the class.
• A static method can’t access or modify class state.
Syntax:
class C(object):
@staticmethod
def fun(arg1, arg2, ...):
...
returns: a static method for function fun.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Class Methods
• A class method is a method that is bound to a class rather than its
object.
• It doesn't require creation of a class instance, much like static method.
• A class method receives the class as implicit first argument, just like an
instance method receives the instance
• It can modify a class state that would apply across all the instances of the
class. For example it can modify a class variable that will be applicable to
all the instances
Syntax:
class C(object):
@classmethod
def fun(cls, arg1, arg2, ...):
....
fun: function that needs to be converted into a class
method returns: a class method for function.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Class Method Vs Static Method
• A class method takes cls as first parameter while a static method needs no specific
parameters.
• A class method can access or modify class state while a static method can’t access or modify it.
• In general, static methods know nothing about class state. They are utility type methods that
take some parameters and work upon those parameters. On the other hand class methods
must have class as parameter.
• We use @classmethod decorator in python to create a class method and we use
@staticmethod decorator to create a static method in python.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Class method Vs Static Method
Uses :
• We generally use class method to create factory methods. Factory methods return
class object ( similar to a constructor ) for different use cases.
• We generally use static methods to create utility functions.
• Python doesn’t support method overloading like C++ or Java so we use class
methods to create factory methods.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method - Factory Method
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method - Factory Method
from datetime import date# random Person
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@classmethod
def fromBirthYear(cls, name, birthYear):
return cls(name, date.today().year - birthYear)
Output :
def display(self):
Adam's age is: 19
print(self.name + "'s age is: " + str(self.age)) John's age is: 34
person = Person('Adam', 19)
person.display()
person1 = Person.fromBirthYear('John', 1985)
person1.display()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method - Factory Method – Explanation
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method
- Correct instance creation in inheritance
• Whenever you derive a class from implementing a factory method
as a class method, it ensures correct instance creation of the
derived class.
• You can create a static method for the above example but the
object it creates, will always be hardcoded as Base class.
• But, when you use a class method, it creates the correct instance of
the derived class.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method
- Correct instance creation in inheritance
from datetime import date # random Person
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
@staticmethod
def fromFathersAge(name, fatherAge, fatherPersonAgeDiff):
return Person(name, date.today().year - fatherAge + fatherPersonAgeDiff) @classmethod
def fromBirthYear(cls, name, birthYear):
return cls(name, date.today().year - birthYear)
def display(self):
print(self.name + "'s age is: " + str(self.age))
class Man(Person):
sex = 'Male‘
man = Man.fromBirthYear('John', 1985)
print(isinstance(man, Man))
Output :
man1 = Man.fromFathersAge('John', 1965, 20)
True
print(isinstance(man1, Man))
False
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Uses of Class Method
- Correct instance creation in inheritance - Explained
• Here, using a static method to create a class instance wants us to
hardcode the instance type during creation.
• fromFathersAge method doesn't return a Man object but its base class
Person's object.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
AGGREGATION AND
DEPENDENCY
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
RELATIONSHIP
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
AGGREGATION
•When one object owns another object, but they both have independent
life cycle.
•Aggregation is a week form of composition. If you delete the container
object contents objects can live without container object.
• If class A owns class B, then class A is said to aggregate class B. This is
also commonly known as "has-A" relationship.
Example :
Customer has an Address. Even if the Customer is no more, there may
be other customers in that address. So Address continues to exist even
after a customer is no more
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
TWO CLASSES CUSTOMER AND ADDRESS
class Customer:
class Address:
def __init__(self, name, age, phone_no): def __init__(self, door_no, street, area,
self.name = name pincode):
self.door_no = door_no
self.age = age self.street = street
self.phone_no = phone_no self.area = area
self.pincode = pincode
def view_details(self): def update_address(self):
pass pass
def update_details(self):
pass
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ADDING EXTRA ATTRIBUTE ADDRESS
class Customer:
def __init__(self, name, age, phone_no,
address):
def __init__(self, door_no, street, area,
self.name = name pincode):
self.door_no = door_no
self.age = age self.street = street
self.phone_no = phone_no self.area = area
self.pincode = pincode
self.address = address
def view_details(self): def update_address(self):
pass
pass
def update_details(self):
pass class Address:
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
AGGREGATION
add1=Address(123,"5th Lane",56001)
add2=Address(567,"6th Lane",82006)
cus1=Customer("Jack",24,1234,None)
cus2=Customer("Jane",25,5678,None)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
AGGREGATION
Since the Customer class has aggregated the Address class, the address object is
available in all the methods of the Customer class, just like regular attributes.
class Customer:
def __init__(self, name, age, phone_no, address):
self.name = name
self.age = age
self.phone_no = phone_no
self.address = address
def view_details(self):
print (self.name, self.age, self.phone_no)
print (self.address.door_no, self.address.street, self.address.pincode)
def update_details(self, add):
self.address = add
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
AGGREGATION
class Address:
def __init__(self, door_no, street, pincode):
self.door_no = door_no
self.street = street
self.pincode = pincode
def update_address(self):
pass
add1=Address(123, "5th Lane", 56001)
add2=Address(567, "6th Lane", 82006)
cus1=Customer("Jack", 24, 1234, add1)
cus1.view_details()
cus1.update_details(add2)
cus1.view_details()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
COMPOSITION
• When one object owns another object, but they both have same life
cycle.
• In composition one of the classes is composed of one or more instance
of other classes.
• In other words one class is container and other class is content and if
you delete the container object then all of its contents objects are also
deleted.
Example:
College has a department. If the college closes, the department is also
closed
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
COMPOSITION
class Salary:
def __init__(self,pay):
self.pay=pay
def get_total(self):
return (self.pay*12)
class Employee:
def __init__(self,pay,bonus):
self.pay=pay
self.bonus=bonus
self.obj_salary=Salary(self.pay)
def annual_salary(self):
return "Total: " + str(self.obj_salary.get_total()+self.bonus)
obj_emp=Employee(100,10)
print (obj_emp.annual_salary())
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example of Inheritance in Python
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
• This class has data attributes to store the number of sides, n and
magnitude of each side as a list, sides.
• Method inputSides() takes in magnitude of each side and
similarly, dispSides() will display these properly.
• A triangle is a polygon with 3 sides. So, we can created a class
called Triangle which inherits from Polygon. This makes all the
attributes available in class Polygon readily available in Triangle. We
don't need to define them again (code re-usability). Triangle is
defined as follows.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
class Triangle(Polygon):
def __init__(self):
Polygon.__init__(self,3)
def findArea(self):
a, b, c = self.sides
# calculate the semi-perimeter
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
>>> t = Triangle()
>>> t.inputSides()
Enter side 1 : 3
Enter side 2 : 5
Enter side 3 : 4
>>> t.dispSides()
Side 1 is 3.0
Side 2 is 5.0
Side 3 is 4.0
>>> t.findArea() The area of the triangle is 6.00
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
METHOD OVERRIDING IN PYTHON
• Override means having two methods with the same name but
doing different tasks. It means that one of the methods overrides the
other.
• If there is any method in the superclass and a method with the same
name in a subclass, then by executing the method, the method of the
corresponding class will be executed.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
METHOD OVERRIDING IN PYTHON
class Rectangle():
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
def getArea(self):
print(self.length*self.breadth," is area of rectangle")
class Square(Rectangle):
def __init__(self,side):
self.side = side
Rectangle.__init__(self,side,side)
def getArea(self):
print(self.side*self.side," is area of square“)
s = Square(4)
r = Rectangle(2,4)
s.getArea()r.getArea()
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
METHOD OVERRIDING IN PYTHON
o/p
16 is area of square
8 is area of rectangle
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
METHOD OVERRIDING IN PYTHON
• Since the method from the coressponding class came into action, it
means that one overrode the other.
• Execution of 'getArea' on the object of Rectangle (r) printed "8 is
area of rectangle" from the 'getArea' defined in the Rectangle class
whereas, execution of 'getArea' on the object of Square (s) printed
"16 is area of square" from the 'getArea' defined in the Square class.
• It is very useful because it prevents us from making methods with
different names and remembering them all
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Different forms of Inheritance:
1. Single inheritance: When a child class inherits from only one parent
class, it is called as single inheritance.
2. Multiple inheritance: When a child class inherits from multiple
parent classes, it is called as multiple inheritance.
3. Multilevel inheritance: When we have child and grand child
relationship.
4. Hierarchical inheritance More than one derived classes are
created from a single base.
5. Hybrid inheritance: This form combines more than one form of
inheritance. Basically, it is a blend of more than one type of
inheritance.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Private members of parent class
• If the coder does not want all the instance variables of the parent
class to be inherited by the child class i.e. some of the instance
variables of the parent class can be made private, which won’t be
available to the child class.
• We can make an instance variable by adding double underscores
before its name
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Private members of parent class
[Super is used to] return a proxy object that delegates method calls
to a parent or sibling class of type. This is useful for accessing
inherited methods that have been overridden in a class. The search
order is same as that used by getattr() except that the type itself is
skipped.”
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SUPER FUNCTION
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SUPER FUNCTION
class MyParentClass():
def __init__(self):
pass
class SubClass(MyParentClass):
def __init__(self):
super()
NOTE: We have to initialize the parent or base class within the subclass
or derived or child. Instead we can call the super() function to process
easier. The goal of Super function is to provide a much more abstract
and portable solution for initializing classes.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SUPER FUNCTION
class Computer():
def __init__(self, computer, ram, ssd):
self.computer = computer
self.ram = ram
self.ssd = ssd
class Laptop(Computer):
def __init__(self, computer, ram, ssd, model):
super().__init__(computer, ram, ssd)
self.model = model
lenovo = Laptop('lenovo', 2, 512, 'l420')
print('This computer is:', lenovo.computer)
print('This computer has ram of', lenovo.ram)
print('This computer has ssd of', lenovo.ssd)
print('This computer has this model:', lenovo.model)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SUPER FUNCTION
O/P
This computer is: lenova
This computer has ram of 2
This computer has ssd of 512
This computer has this model 1420
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Class
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Class - Example
class Base: OUTPUT 1:
def foo(self): >>> b = Base()
raise NotImplementedError()
>>> b.foo()
NotImplementedError
def bar(self):
raise NotImplementedError()
class Concrete(Base):
def foo(self):
return 'foo() called'
OUTPUT 2:
instantiating and using Concrete works as expected. And, if we call an
unimplemented method like bar() on it, this also raises an exception:
>>> c = Concrete()
>>> c.foo()
'foo() called'
>>> c.bar()
NotImplementedError
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Creating an Abstract Base Class (abc)
• Python does not support Abstraction
• Python comes with a module which provides the infrastructure for
defining Abstract Base Classes (ABCs)
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example - Abstract Base Class (abc)
from abc import ABCMeta, abstractmethod
Subclasses of Base raise a TypeError
at instantiation time whenever we
class Base(metaclass=ABCMeta): forget to implement any abstract
@abstractmethod methods. The raised exception tells us
which method or methods we’re
def foo(self): missing
pass
@abstractmethod
def bar(self):
pass
OUTPUT :
class Concrete(Base): >>> c = Concrete()
def foo(self): c.bar()
TypeError:
print(“hi”) "Can't instantiate abstract class Concrete \
with abstract methods bar"
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Base Class
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Class – ABC Package
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Exception
• Program terminates as soon as it encounters an error.
• Syntax error or an Exception
Exception :
Occurs whenever syntactically correct Python code results in an
error.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Revising - Handling Exception – finally – Cleans up
Syntax :
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Raising an Exception
• raise – used to throw an exception if a condition occurs.
• The statement can be complemented with a custom exception.
SAMPLE :
x = 10
if x > 5:
raise Exception('x should not exceed 5. The value of x was: {}'.format(x))
Output :
Traceback (most recent call last):
File "<input>", line 4, in <module>
Exception: x should not exceed 5. The value of x was: 10
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
The AssertionError Exception
import sys
assert ('linux' in sys.platform), "This code runs on Linux only."
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Assertion Demo Code
def apply_discount(product, discount):
price = int(product['price'] * (1.0 - discount))
assert 0 <= price <= product['price']
return price
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.