SlideShare ist ein Scribd-Unternehmen logo
1 von 23
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

Weitere ähnliche Inhalte

Was ist angesagt?

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming Raghunath A
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and PackagesDamian T. Gordon
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Kamlesh Makvana
 
python conditional statement.pptx
python conditional statement.pptxpython conditional statement.pptx
python conditional statement.pptxDolchandra
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in PythonSujith Kumar
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python TutorialSimplilearn
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on pythonwilliam john
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in pythonSantosh Verma
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In JavaSpotle.ai
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++Neeru Mittal
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 

Was ist angesagt? (20)

Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function Virtual function in C++ Pure Virtual Function
Virtual function in C++ Pure Virtual Function
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
python conditional statement.pptx
python conditional statement.pptxpython conditional statement.pptx
python conditional statement.pptx
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Presentation on python
Presentation on pythonPresentation on python
Presentation on python
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Java input
Java inputJava input
Java input
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 

Andere mochten auch

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
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
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 程式語言起步走台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹台灣資料科學年會
 

Andere mochten auch (15)

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
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
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爬蟲實戰
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
 

Ähnlich wie 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
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 

Ähnlich wie 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#
 
Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 

Kürzlich hochgeladen

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Kürzlich hochgeladen (20)

Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

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