SlideShare a Scribd company logo
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
Most read
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
Most read
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
Most read
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 (20)

Python libraries
Python librariesPython libraries
Python libraries
Prof. Dr. K. Adisesha
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
Emertxe Information Technologies Pvt Ltd
 
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
Sujith Kumar
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
Niloy Saha
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
Edureka!
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
Emertxe Information Technologies Pvt Ltd
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
 
Python Programming
Python ProgrammingPython Programming
Python Programming
Saravanan T.M
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
baabtra.com - No. 1 supplier of quality freshers
 
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
Sujith Kumar
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
Niloy Saha
 
Python Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | EdurekaPython Basics | Python Tutorial | Edureka
Python Basics | Python Tutorial | Edureka
Edureka!
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
Simplilearn
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
 

Viewers also liked (13)

Python 2 vs. Python 3
Python 2 vs. Python 3Python 2 vs. Python 3
Python 2 vs. Python 3
Pablo Enfedaque
 
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
Audrey 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
 
型態與運算子
型態與運算子型態與運算子
型態與運算子
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 Minutes
Matt Harrison
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰
台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
台灣資料科學年會
 
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
Audrey 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 Minutes
Matt Harrison
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走
台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
台灣資料科學年會
 
Ad

Similar to Advance OOP concepts in Python (20)

Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Inheritance Super and MRO _
Inheritance Super and MRO               _Inheritance Super and MRO               _
Inheritance Super and MRO _
swati463221
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai
 
Inheritance
InheritanceInheritance
Inheritance
JayanthiNeelampalli
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Presentation_4516_Content_Document_20250204010703PM.pptx
Presentation_4516_Content_Document_20250204010703PM.pptxPresentation_4516_Content_Document_20250204010703PM.pptx
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
Jyoti shukla
 
Lecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad HaroonLecture # 02 - OOP with Python Language by Muhammad Haroon
Lecture # 02 - OOP with Python Language by Muhammad Haroon
National College of Business Administration & Economics ( NCBA&E)
 
Python advance
Python advancePython advance
Python advance
Mukul Kirti Verma
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
SmritiSharma901052
 
9-_Object_Oriented_Programming_Using_Python 1.pptx
9-_Object_Oriented_Programming_Using_Python 1.pptx9-_Object_Oriented_Programming_Using_Python 1.pptx
9-_Object_Oriented_Programming_Using_Python 1.pptx
Lahari42
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Object Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdfObject Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdf
RishuRaj953240
 
9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
anaveenkumar4
 
Object-Oriented Programming (OO) in pythonP) in python.pptx
Object-Oriented Programming (OO)  in pythonP)  in python.pptxObject-Oriented Programming (OO)  in pythonP)  in python.pptx
Object-Oriented Programming (OO) in pythonP) in python.pptx
institute of Geoinformatics and Earth Observation at PMAS ARID Agriculture University of Rawalpindi
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Koteswari Kasireddy
 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
alaparthi
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Inheritance Super and MRO _
Inheritance Super and MRO               _Inheritance Super and MRO               _
Inheritance Super and MRO _
swati463221
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdfUnit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.Object oriented Programning Lanuagues in text format.
Object oriented Programning Lanuagues in text format.
SravaniSravani53
 
Presentation_4516_Content_Document_20250204010703PM.pptx
Presentation_4516_Content_Document_20250204010703PM.pptxPresentation_4516_Content_Document_20250204010703PM.pptx
Presentation_4516_Content_Document_20250204010703PM.pptx
MuhammadChala
 
object oriented programming(PYTHON)
object oriented programming(PYTHON)object oriented programming(PYTHON)
object oriented programming(PYTHON)
Jyoti shukla
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
OOP concepts -in-Python programming language
OOP concepts -in-Python programming languageOOP concepts -in-Python programming language
OOP concepts -in-Python programming language
SmritiSharma901052
 
9-_Object_Oriented_Programming_Using_Python 1.pptx
9-_Object_Oriented_Programming_Using_Python 1.pptx9-_Object_Oriented_Programming_Using_Python 1.pptx
9-_Object_Oriented_Programming_Using_Python 1.pptx
Lahari42
 
Python programming computer science and engineering
Python programming computer science and engineeringPython programming computer science and engineering
Python programming computer science and engineering
IRAH34
 
Object Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdfObject Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdf
RishuRaj953240
 
9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
anaveenkumar4
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Koteswari Kasireddy
 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
alaparthi
 
Ad

Recently uploaded (20)

vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
GIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of UtilitiesGIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of Utilities
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
GIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of UtilitiesGIS and FME: The Foundation to Improve the Locate Process of Utilities
GIS and FME: The Foundation to Improve the Locate Process of Utilities
Safe Software
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 

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