SlideShare a Scribd company logo
1 of 23
Download to read offline
7/22/2014VYBHAVA TECHNOLOGIES 1
 Muliple Inheritance
 Multilevel Inheritance
 Method Resolution Order (MRO)
 Method Overriding
 Methods Types
Static Method
Class Method
7/22/2014VYBHAVA TECHNOLOGIES 2
 Multiple inheritance is possible in Python.
 A class can be derived from more than one base classes. The
syntax for multiple inheritance is similar to single inheritance.
 Here is an example of multiple inheritance.
Syntax:
class Base1:
Statements
class Base2:
Statements
class MultiDerived(Base1, Base2):
Statements
7/22/2014VYBHAVA TECHNOLOGIES 3
7/22/2014VYBHAVA TECHNOLOGIES 4
 On the other hand, we can inherit form a derived class.
 This is also called multilevel inheritance.
 Multilevel inheritance can be of any depth in Python.
 An example with corresponding visualization is given below.
Syntax:
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
pass
7/22/2014VYBHAVA TECHNOLOGIES 5
The class Derivedd1 inherits from
Base and Derivedd2 inherits from
both Base as well as Derived1
7/22/2014VYBHAVA TECHNOLOGIES 6
 Every class in Python is derived from the class object.
 In the multiple inheritance scenario, any specified attribute is
searched first in the current class. If not found, the search
continues into parent classes in depth-first, left-right fashion
without searching same class twice.
 So, in the above example of MultiDerived class the search order
is [MultiDerived, Base1, Base2, object].
 This order is also called linearization of MultiDerived class and
the set of rules used to find this order is called Method
Resolution Order (MRO)
continue…
7/22/2014VYBHAVA TECHNOLOGIES 7
…continue
 MRO must prevent local precedence ordering and also provide
monotonicity.
 It ensures that a class always appears before its parents and in case of
multiple parents, the order is same as tuple of base classes.
 MRO of a class can be viewed as the __mro__ attribute
or mro() method. The former returns a tuple while latter returns a list
>>>MultiDerived.__mro__
(<class '__main__.MultiDerived'>,
<class '__main__.Base1'>,
<class '__main__.Base2'>,
<class 'object'>)
>>> MultiDerived.mro()
[<class '__main__.MultiDerived'>,
<class '__main__.Base1'>,
<class '__main__.Base2'>,
<class 'object'>]
7/22/2014VYBHAVA TECHNOLOGIES 8
Here is a little more complex multiple inheritance example and its
visualization along with the MRO.
Example:
class X:
pass
class Y:
pass
class Z:
pass
class A(X,Y): pass
class B(Y,Z): pass
class M(B,A,Z): pass
print(M.mro())
7/22/2014VYBHAVA TECHNOLOGIES 9
7/22/2014VYBHAVA TECHNOLOGIES 10
 The subclasses can override the logic in a superclass,
allowing you to change the behavior of your classes without
changing the superclass at all.
 Because changes to program logic can be made via
subclasses, the use of classes generally supports code reuse
and extension better than traditional functions do.
 Functions have to be rewritten to change how they work
whereas classes can just be subclassed to redefine methods.
7/22/2014VYBHAVA TECHNOLOGIES 11
class FirstClass: #define the super class
def setdata(self, value): # define methods
self.data = value # ‘self’ refers to an instance
def display(self):
print self.data
class SecondClass(FirstClass): # inherits from FirstClass
def display(self): # redefines display
print 'Current Data = %s' % self.data
x=FirstClass() # instance of FirstClass
y=SecondClass() # instance of SecondClass
x.setdata('Before Method Overloading')
y.setdata('After Method Overloading')
x.display()
y.display()
7/22/2014VYBHAVA TECHNOLOGIES 12
 Both instances (x and y) use the same setdata method from
FirstClass; x uses it because it’s an instance of FirstClass
while y uses it because SecondClass inherits setdata from
FirstClass.
 However, when the display method is called, x uses the
definition from FirstClass but y uses the definition from
SecondClass, where display is overridden.
7/22/2014VYBHAVA TECHNOLOGIES 13
7/22/2014VYBHAVA TECHNOLOGIES 14
 It Possible to define two kinds of methods with in a class that
can be called without an instance
1) static method
2) class method
 Normally a class method is passed ‘self’ as its first argument.
Self is an instance object
 Some times we need to process data associated with instead
of instances
 Let us assume, simple function written outside the class, the
code is not well associated with class, can’t be inherited and
the name of the method is not localized
 Hence python offers us static and class methods
7/22/2014VYBHAVA TECHNOLOGIES 15
> Simple function with no self argument
> Nested inside class
> Work on class attribute not on instance attributes
> Can be called through both class and instance
> The built in function static method is used to create
them
7/22/2014VYBHAVA TECHNOLOGIES 16
class MyClass:
def my_static_method():
-------------------------
----rest of the code---
my_static_method=staticmethod (my_static_method)
7/22/2014VYBHAVA TECHNOLOGIES 17
class Students(object):
total = 0
def status():
print 'n Total Number of studetns is :', Students.total
status= staticmethod(status)
def __init__(self, name):
self.name= name
Students.total+=1
print ‘Before Creating instance: ‘, Students.total
student1=Students('Guido')
student2=Students('Van')
student3=Students('Rossum')
Students.status() # Accessing the class attribute through direct class name
student1.status() # Accessing the class attribute through an object
7/22/2014VYBHAVA TECHNOLOGIES 18
7/22/2014VYBHAVA TECHNOLOGIES 19
 Functions that have first argument as class name
 Can be called through both class and instance
 These are created with classmethod() inbuilt function
 These always receive the lowest class in an instance’s
tree
7/22/2014VYBHAVA TECHNOLOGIES 20
class MyClass:
def my_class_method(class _ var):
-------------------------
----rest of the code---
my_class_method=classmethod (my_class_method)
7/22/2014VYBHAVA TECHNOLOGIES 21
class Spam:
numinstances = 0
def count(cls):
cls.numinstances +=1
def __init__(self):
self.count()
count=classmethod(count) # Converts the count function to class method
class Sub(Spam):
numinstances = 0
class Other(Spam):
numinstances = 0
S= Spam()
y1,y2=Sub(),Sub()
z1,z2,z3=Other(),Other(),Other()
print S.numinstances, y1.numinstances,z1.numinstances
print Spam.numinstances, Sub.numinstances,Other.numinstances
7/22/2014VYBHAVA TECHNOLOGIES 22
7/22/2014VYBHAVA TECHNOLOGIES 23

More Related Content

What's hot

Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For SyntaxPravinYalameli
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 

What's hot (20)

Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
OOP java
OOP javaOOP java
OOP java
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Java Presentation For Syntax
Java Presentation For SyntaxJava Presentation For Syntax
Java Presentation For Syntax
 
Java applet
Java appletJava applet
Java applet
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Sql injection
Sql injectionSql injection
Sql injection
 
Oop java
Oop javaOop java
Oop java
 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 

Viewers also liked

Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutAudrey Roy
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 
Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影Tim (文昌)
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn Cicilia Lee
 
常用內建模組
常用內建模組常用內建模組
常用內建模組Justin Lin
 
型態與運算子
型態與運算子型態與運算子
型態與運算子Justin Lin
 
Python 起步走
Python 起步走Python 起步走
Python 起步走Justin Lin
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesMatt Harrison
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹台灣資料科學年會
 

Viewers also liked (14)

Python 2 vs. Python 3
Python 2 vs. Python 3Python 2 vs. Python 3
Python 2 vs. Python 3
 
Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
 
Python 2-基本語法
Python 2-基本語法Python 2-基本語法
Python 2-基本語法
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
 
常用內建模組
常用內建模組常用內建模組
常用內建模組
 
進階主題
進階主題進階主題
進階主題
 
型態與運算子
型態與運算子型態與運算子
型態與運算子
 
Python 起步走
Python 起步走Python 起步走
Python 起步走
 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走
 
[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
 

Similar to Advance OOP concepts in Python

Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxnaeemcse
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
Application package
Application packageApplication package
Application packageJAYAARC
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Javabackdoor
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxkristinatemen
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming RailsJustus Eapen
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Framework prototype
Framework prototypeFramework prototype
Framework prototypeDevMix
 
Java assignment help
Java assignment helpJava assignment help
Java assignment helpJacob William
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingKhadijaKhadijaAouadi
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
Group 9(java method overriding).pdf
Group 9(java method overriding).pdfGroup 9(java method overriding).pdf
Group 9(java method overriding).pdfHirkoGemechu
 

Similar to Advance OOP concepts in Python (20)

Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Application package
Application packageApplication package
Application package
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Inheritance
InheritanceInheritance
Inheritance
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
 
Only oop
Only oopOnly oop
Only oop
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Framework prototype
Framework prototypeFramework prototype
Framework prototype
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programming
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Group 9(java method overriding).pdf
Group 9(java method overriding).pdfGroup 9(java method overriding).pdf
Group 9(java method overriding).pdf
 

Recently uploaded

Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsSafe Software
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 

Recently uploaded (20)

20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration WorkflowsIgniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 

Advance OOP concepts in Python

  • 2.  Muliple Inheritance  Multilevel Inheritance  Method Resolution Order (MRO)  Method Overriding  Methods Types Static Method Class Method 7/22/2014VYBHAVA TECHNOLOGIES 2
  • 3.  Multiple inheritance is possible in Python.  A class can be derived from more than one base classes. The syntax for multiple inheritance is similar to single inheritance.  Here is an example of multiple inheritance. Syntax: class Base1: Statements class Base2: Statements class MultiDerived(Base1, Base2): Statements 7/22/2014VYBHAVA TECHNOLOGIES 3
  • 5.  On the other hand, we can inherit form a derived class.  This is also called multilevel inheritance.  Multilevel inheritance can be of any depth in Python.  An example with corresponding visualization is given below. Syntax: class Base: pass class Derived1(Base): pass class Derived2(Derived1): pass 7/22/2014VYBHAVA TECHNOLOGIES 5
  • 6. The class Derivedd1 inherits from Base and Derivedd2 inherits from both Base as well as Derived1 7/22/2014VYBHAVA TECHNOLOGIES 6
  • 7.  Every class in Python is derived from the class object.  In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching same class twice.  So, in the above example of MultiDerived class the search order is [MultiDerived, Base1, Base2, object].  This order is also called linearization of MultiDerived class and the set of rules used to find this order is called Method Resolution Order (MRO) continue… 7/22/2014VYBHAVA TECHNOLOGIES 7
  • 8. …continue  MRO must prevent local precedence ordering and also provide monotonicity.  It ensures that a class always appears before its parents and in case of multiple parents, the order is same as tuple of base classes.  MRO of a class can be viewed as the __mro__ attribute or mro() method. The former returns a tuple while latter returns a list >>>MultiDerived.__mro__ (<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>) >>> MultiDerived.mro() [<class '__main__.MultiDerived'>, <class '__main__.Base1'>, <class '__main__.Base2'>, <class 'object'>] 7/22/2014VYBHAVA TECHNOLOGIES 8
  • 9. Here is a little more complex multiple inheritance example and its visualization along with the MRO. Example: class X: pass class Y: pass class Z: pass class A(X,Y): pass class B(Y,Z): pass class M(B,A,Z): pass print(M.mro()) 7/22/2014VYBHAVA TECHNOLOGIES 9
  • 11.  The subclasses can override the logic in a superclass, allowing you to change the behavior of your classes without changing the superclass at all.  Because changes to program logic can be made via subclasses, the use of classes generally supports code reuse and extension better than traditional functions do.  Functions have to be rewritten to change how they work whereas classes can just be subclassed to redefine methods. 7/22/2014VYBHAVA TECHNOLOGIES 11
  • 12. class FirstClass: #define the super class def setdata(self, value): # define methods self.data = value # ‘self’ refers to an instance def display(self): print self.data class SecondClass(FirstClass): # inherits from FirstClass def display(self): # redefines display print 'Current Data = %s' % self.data x=FirstClass() # instance of FirstClass y=SecondClass() # instance of SecondClass x.setdata('Before Method Overloading') y.setdata('After Method Overloading') x.display() y.display() 7/22/2014VYBHAVA TECHNOLOGIES 12
  • 13.  Both instances (x and y) use the same setdata method from FirstClass; x uses it because it’s an instance of FirstClass while y uses it because SecondClass inherits setdata from FirstClass.  However, when the display method is called, x uses the definition from FirstClass but y uses the definition from SecondClass, where display is overridden. 7/22/2014VYBHAVA TECHNOLOGIES 13
  • 15.  It Possible to define two kinds of methods with in a class that can be called without an instance 1) static method 2) class method  Normally a class method is passed ‘self’ as its first argument. Self is an instance object  Some times we need to process data associated with instead of instances  Let us assume, simple function written outside the class, the code is not well associated with class, can’t be inherited and the name of the method is not localized  Hence python offers us static and class methods 7/22/2014VYBHAVA TECHNOLOGIES 15
  • 16. > Simple function with no self argument > Nested inside class > Work on class attribute not on instance attributes > Can be called through both class and instance > The built in function static method is used to create them 7/22/2014VYBHAVA TECHNOLOGIES 16
  • 17. class MyClass: def my_static_method(): ------------------------- ----rest of the code--- my_static_method=staticmethod (my_static_method) 7/22/2014VYBHAVA TECHNOLOGIES 17
  • 18. class Students(object): total = 0 def status(): print 'n Total Number of studetns is :', Students.total status= staticmethod(status) def __init__(self, name): self.name= name Students.total+=1 print ‘Before Creating instance: ‘, Students.total student1=Students('Guido') student2=Students('Van') student3=Students('Rossum') Students.status() # Accessing the class attribute through direct class name student1.status() # Accessing the class attribute through an object 7/22/2014VYBHAVA TECHNOLOGIES 18
  • 20.  Functions that have first argument as class name  Can be called through both class and instance  These are created with classmethod() inbuilt function  These always receive the lowest class in an instance’s tree 7/22/2014VYBHAVA TECHNOLOGIES 20
  • 21. class MyClass: def my_class_method(class _ var): ------------------------- ----rest of the code--- my_class_method=classmethod (my_class_method) 7/22/2014VYBHAVA TECHNOLOGIES 21
  • 22. class Spam: numinstances = 0 def count(cls): cls.numinstances +=1 def __init__(self): self.count() count=classmethod(count) # Converts the count function to class method class Sub(Spam): numinstances = 0 class Other(Spam): numinstances = 0 S= Spam() y1,y2=Sub(),Sub() z1,z2,z3=Other(),Other(),Other() print S.numinstances, y1.numinstances,z1.numinstances print Spam.numinstances, Sub.numinstances,Other.numinstances 7/22/2014VYBHAVA TECHNOLOGIES 22