Sie sind auf Seite 1von 99

Session -

OOPS - Python

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECT ORIENTED PROGRAMMING

•Object Oriented Programming is a popular methodology of


programming any application.
•Python, is an Object Oriented Programming language.
•Object Oriented programming is a programming style that is
associated with the concept of Class, Objects and various other
concepts revolving around these two, like Inheritance,
Polymorphism, Abstraction, Encapsulation etc.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OOP

• OOP is designed in such a way that one should focus on an object


while programming and not the procedure.
• An object can be anything that we see around us. It can be a human
(that has some properties like - name, address, DOB and so on), a
chair (portrayed by size, material, cost etc), a school (depicted by
place, student strength, results) etc.
• Object oriented programming brings programming close to real life,
as we are always dealing with an object, performing operations on it,
using it's methods and variables etc.

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

1) OOP provides a clear modular structure for programs


2) OOP makes it easy to maintain and modify existing code
3) OOP provides a good framework for code libraries where
supplied software components can be easily adapted and
modified by the programmer.
4)Code Resusablity
5)It is suitable for realword problems and real world works

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

• A class is a template or blueprint that is used to create objects.


• Class representation of objects and the sets of operations that can be
applied to such objects.
• A class consists of Data members and methods.
• The primary purpose of a class is to hold data/information. This is
achieved with attributes which are also known as data members.
• The member functions determine the behavior of the class, i.e.
provide a definition for supporting various operations on data held
in the form of an object.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASS

• The class is a group of similar entities.


• It is only an logical component and not the physical entity.
• For example, if you had a class called “Expensive Cars” it could have
objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be
price or speed of these cars. While the methods may be performed
with these cars are driving, reverse, braking etc.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECTS

In real-world an entity that has state and its behavior is known as an


object.
For Example:
• A Car is an object. It has states (name, color, model) and its behavior
(changing gear, applying brakes).
• A Pen is an object. Its name is Parker; color is silver etc. known as its
state. It is used to write, so writing is its behavior.
In real-world object and software object have conceptually similar
characteristics. In terms of object-oriented programming, software
objects also have a state and behavior.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
OBJECTS

• An object can be defined as an instance of a class, and there can be


multiple instances of a class in a program.
• An Object contains both the data and the function, which operates
on the data. For example - chair, bike, marker, pen, table, car, etc.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ABSTRACTION

• Abstraction is the concept of hiding the internal details


(implementation) and describing things in simple terms.
• Revealing relevant/necessary information and hiding the unwanted
information is abstraction.
EXAMPLE
• In a mobile phone, dialing a number would call some method internally
which will concatenate the numbers and displays it on screen but, we
don’t know what is happening internally.
• When you tap on the call option, it is sending signals to other person’s
mobile(whom you are trying to call) but we are unaware of it’s
implementation.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
ENCAPSULATION

• Encapsulation is the technique used to implement abstraction in object-


oriented programming. Encapsulation is used for access restriction to
class members and methods.
• Enclosing one or more details from outside world through access rights.
EXAMPLE
• We have Bluetooth in our mobiles to transfer data between two mobile
phones within a limited distance.
• If I turn on Bluetooth in both of the mobiles, I can transfer the desired
data, but I can’t access the other mobile’s information like gallery until
and unless I get a permission from the other mobile.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
POLYMORPHISM

• Polymorphism is the concept where an object behaves differently in


different situations.
• Performing a job in different ways.(One to many)
EXAMPLE
• We click photographs and record videos using the camera in a mobile
phone.
• Various brands have a plethora of camera modes in a mobile like: split
camera/panaroma/slow-mo.
• The basic purpose is to click/record, so the functionality of camera is the
same in every mobile, whereas the modes enable you to use your camera
in different ways.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE

• Inheritance is the object oriented programming concept where an


object is based on another object
• Inheritance is the mechanism of code reuse.
• The object that is getting inherited is called superclass and the object
that inherits the superclass is called subclass.
Example:
• The basic purpose of using a mobile phone is communication.
• There are several brands in mobiles. So, the brands of a mobile are
using this basic functionality(communication) by extending the mobile
class functionality and adding their own new features to their respective
brands.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
CLASSES IN PYTHON

• Classes are created by keyword class.


• Attributes are the variables that belong to class.
• Attributes are always public and can be accessed using dot (.)
operator. Eg.: Myclass.Myattribute
• Usual function definition without a class is similar to this except for
the def keyword and a new parameter named self.
• Self is a mandatory parameter for every function defined in a class
that represents the current active object of the class, using which the
function of the class is called

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

def function2(self, parameter1, ...):


self.variable2 = something else
function2 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

Class is mere a blueprint or a template. No storage is assigned when


we define a class. Objects are instances of class, which holds the data
variables declared in the class and the member functions work on
these class objects.
To create an object,
myObject = MyClass()

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

• 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 class is created.
• In Python the __init__() method is called the constructor and is
always called when an object is created.

Syntax of constructor declaration :


def __init__(self):
# body of the constructor

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Init METHOD

• The __init__ method


The __init__ method is similar to constructors in C++ and Java. It is run as
soon as an object of a class is instantiated. The method is useful to do any
initialization you want to do with your object.

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

class Addition: def calculate(self):


first = 0 self.answer = self.first + self.second
second = 0
answer = 0 # creating object of the class
# parameterized constructor
# this will invoke parameterized constructor
obj = Addition(1000, 2000)
def __init__(self, f, s):
self.first = f # perform Addition
self.second = s obj.calculate()

def display(self): # display result


print("First number = " + str(self.first)) obj.display()
print("Second number = " + str(self.second))
print("Addition of two numbers = " + str(self.answer))

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 &lt;module&gt;
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

• A single underscore: Private variable, it should not be accessed


directly. But nothing stops you from doing that (except convention).
• A double underscore: Private variable, harder to access but still
possible.
• Both are still accessible: Python has private variables by convention.

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

You can delete properties on objects by using the del keyword:


class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name) print(p1.age)
p1 = Person("John", 36) AttributeError: 'Person'
object has no attribute
del p1.age 'age'
print(p1.age)

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Delete Objects

You can delete objects by using the del keyword


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)
print(p1)
del p1
NameError: 'p1' is not
print(p1) defined

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

A string object with the value and makes reference


that.

>>>n=“foo”

The allocated memory is reclaim so it can be used


for something else

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Object identity

• Every object that is created is given a number that uniquely


identifies it.
• Python function id() returns an object’s integer identifier

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

>>> m = 400 different objects with different identities.


>>> id(m)
60127872

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)

list_of_mobiles=[mob1, mob2, mob3, mob4, mob5]

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"

# Class variables can be accessed using class name also


print(CSStudent.stream) # prints "cse"

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

• Static variable can act as a private variable by adding a double


underscore in front of it.
• Create getter and setter methods to access or modify it.
• Self is not used inside the method to access a static variable which is
private.
• Static variable is object independent

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

• Factory methods are those methods which return a class object


(like constructor) for different use cases.
• It is similar to function overloading in C++. Since, Python doesn't
have anything as such, class methods and static methods are used.

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

• Here, we have two class instance creator, a constructor and a


fromBirthYear method.
• Constructor takes normal parameters name and age. While,
fromBirthYear takes class, name and birthYear, calculates the current
age by subtracting it with the current year and returns the class
instance.
• The fromBirthYear method takes Person class (not Person object) as the
first parameter cls and returns the constructor by calling cls(name,
date.today().year - birthYear), which is equivalent to Person(name,
date.today().year - birthYear)
• Before the method, we see @classmethod. This is called a decorator for
converting fromBirthYear to a class method as classmethod().

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.

• This clearly causes a problem when inheriting Person to Man.

• fromFathersAge method doesn't return a Man object but its base class
Person's object.

• This violates OOP paradigm. Using a class method as fromBirthYear can


ensure the OOP-ness of the code since it takes the first parameter as the
class itself and calls its factory method.

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

• Composition and aggregation are specialised form of Association


• Whereas Association is a relationship between two classes
without any rules.

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

Inheritance is the capability of one class to derive or inherit the properties


from some another class. The benefits of inheritance are:
• It represents real-world relationships well.
• It provides reusability of a code. We don’t have to write the same code
again and again. Also, it allows us to add more features to a class
without modifying it.
• It is transitive in nature, which means that if class B inherits from
another class A, then all the subclasses of B would automatically inherit
from class A.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
INHERITANCE

Python Inheritance Syntax

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

A polygon is a closed figure with 3 or more sides. Say, we have a class


called Polygondefined as follows.
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides self.sides = [0 for i in range(no_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in
range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])

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

• We can see that, even though we did not define methods


like inputSides() or dispSides()for class Triangle, we were able to use
them.
• If an attribute is not found in the class, search continues to the base
class. This repeats recursively, if the base class is itself derived from
other classes.

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

# Python program to demonstrate private members of the parent class


class C(object):
def __init__(self):
self.c = 21
# d is private instance variable
self.__d = 42
class D(C): AttributeError: type object 'D' has
no attribute 'd'
def __init__(self):
self.e = 84
C.__init__(self)
object1 = D()
# produces an error as d is private instance variable
print D.d
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SUPER FUNCTION

[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

The super function is somewhat versatile, and can be used in a couple of


ways.
• Use Case 1: Super can be called upon in a single inheritance, in order to
refer to the parent class or multiple classes without explicitly naming
them. It’s somewhat of a shortcut, but more importantly, it helps keep
your code maintainable for the foreseeable future.
• Use Case 2: Super can be called upon in a dynamic execution
environment for multiple or collaborative inheritance. This use is
considered exclusive to Python, because it’s not possible with languages
that only support single inheritance or are statically compiled.

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

NOTE: A Python super() will always refer the immediate superclass


(Multilevel inheritance)

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Class

• Abstract classes are classes that contain one or more abstract


methods.
• An abstract method is a method that is declared, but contains no
implementation.
• Abstract classes may not be instantiated, and require subclasses to
provide implementations for the abstract methods.
• Subclasses of an abstract class in Python are not required to
implement abstract methods of the parent 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

• Without abc, we’d only get a NotImplementedError if a missing


method was actually called. Being notified about missing methods at
instantiation time is a great advantage

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Class – ABC Package

• Creating an abstract class in this way prevents improper usage of


methods that are not overriden, and certainly encourages methods
to be defined in child classes, but it does not enforce their definition
• With the abc module we can prevent child classes from being
instantiated when they fail to override abstract class methods of
their parents

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

• Make an assertion instead of waiting for program to crash.


• We assert that a certain condition is met.
• If this condition turns out to be True, then that is excellent! The
program can continue. If the condition turns out to be False, you can
have the program throw an AssertionError exception.

import sys
assert ('linux' in sys.platform), "This code runs on Linux only."

Traceback (most recent call last):


File "<input>", line 2, in <module>
AssertionError: 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

shoes = {'name': 'Fancy Shoes', 'price': 14900} OUTPUT 1:


11175
apply_discount(shoes, 0.25)
print(apply_discount(shoes, 0.25))
#print(apply_discount(shoes, 2.0)) OUTPUT 2 & 3:
#print(apply_discount(shoes, -0.3)) Traceback (most recent call last):
File "main.py", line 9, in <module>
print(apply_discount(shoes, 2.0))
File "main.py", line 3, in apply_discount
assert 0 <= price <= product['price']
AssertionError

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.

Das könnte Ihnen auch gefallen