SlideShare a Scribd company logo
Mixing it all up with Python
We shall discuss
● Classes
● New style classes
● Inheritace heirarchy and MRO
● Mixins
● Meta Classes
Classes
● Blueprints for creating an object
● Objects have properties(attributes) and
actions(methods)
● Python Classes are a little more than that, but
we shall see that later on
● Lets discuss on class definition
Class
class MyClass(BaseClass1, BaseClass2):
def __init__(self, attr1, attr2):
self.attr1 = attr1
self.attr2 = attr2
def do_something(self):
Print 'doing something...'
Stuff to discuss
● If there are no base classes you need not include
the braces after the class name
● object initialization is done in __init__(constructor)
● first argument to the method is the object itself.
Although it is customary to call the first argument
'self' it is not necessary
● The object has already been created by the time
__init__ is called
New style classes
● Introduced in python 2.2
● Inherit from 'object'
● Yes 'object' is actually a class, which act as the
base class of new style classes
● And yes in python a class is also an object
● Never mind...
● In python 3 all classes are new style even if you
do not explicitly inherit 'object'
New style class
class MyClass(object):
def __init__(self, attr1, attr2):
super(MyClass, self).__init__()
self.attr1 = attr1
self.attr2 = attr2
def do_something(self):
Print 'doing something...'
What is the benifit of new style
classes
● type class unification
● you can now subclass list, dict etc and create a
custom type that has all qualities of a builtin
with some extra functionality
● Follow on for more benifits
Old vs New
class X:
def __init__(self, value):
self.value = value
def __eq__(self, other):
return self.value == other.value
class X(object):
def __init__(self, value):
super(X, self).__init__()
self.value = value
def __eq__(self, other):
return self.value == other.value
Beginners better not
● If you want to alter the object creation you are looking
for __new__()
● Unlike __init__, __new__ is a staticmethod that
receives the class itself as its first argument.
● This is used to alter the object creation process itself
● Used only when you have to subclass imutables
● The __init__ method of the newly created object is
invoked only if __new__ returns an object of the
class's type
example
class X(str):
def __new__(cls, arg1):
return str.__new__(cls, arg1.upper())
def __init__(self, my_text):
self.original_text = my_text
Class Customization
● You can customize your classes with
– __eq__, __ne__, __lt__, __gt__, __le__, __ge__,
__cmp__,
– __nonzero__ called when you do bool(yourObject)
– If __nonzero__ does not exist __len__ is tried
– If both do not exist all values are assumed to be
possitive
● reference
MRO
● Defines where the interpreter looks, in the
inheritance chain, for a method when invoked
on an object
● It is the same logic for attributes although it is
named MRO
● Python 2.2 would do a depth first left to right
C3 algorithm
● Introduced in 2.3
● A MRO is bad when it breaks such fundamental properties
as
– local precedence ordering
● The order of base classes as defined by user
– Monotonicity
● A MRO is monotonic when the following is true: if C1 precedes C2 in the
linearization of C, then C1 precedes C2 in the linearization of any subclass
of C
● 2.3 Will raise an TypeError when you define a bad MRO
● reference
A Bad MRO
class X(object):
pass
class Y(object):
pass
class A(X, Y):
pass
class B(Y, X):
pass
class C(A, B):
pass
C3 Algorithm
take the head of the first list, if this head is not in the
tail of any of the other lists, then add it to the
linearization of C and remove it from the lists in the
merge, otherwise look at the head of the next list
and take it, if it is a good head. Then repeat the
operation until all the class are removed or it is
impossible to find good heads. In this case, it is
impossible to construct the merge, Python 2.3 will
refuse to create the class C and will raise an
exception.
Appy to our example
MRO(A) = AXYO
MRO(B) = BYXO
MRO(C) = C , AXYO, BYXO, AB
MRO(C) = C, A, XYO, BYXO, B
MRO(C) = C, A, B, XYO, YXO
Error- X appears in tail position in YXO and Y appears in
tail position in XYO
Mixins
● A form of inheritance where your bases implement a few
methods
– That can be mixed with out affecting the inheriting class, but
adding more features
– That may call methods that may not be defined in the same class
● Mixins are not classes that can be instantiated
● Used when implementing a logic involving optional
features, extended features etc
● Not very usefull unless you want to create a class from a
bunch of mixin classes at run time
Meta Classes
● It is the class of a class
● Allows creation of class at run time
● Possible because in python everything is an
object includng class and hence can be created
and modified at run time
● 'type()' is not a function, its a metaclass
● And yes metaclass can be subclassed
● reference
examples
a = str('hello world')
type(a)
type(type(a))
str.__class__
new_str = type('new_string_type', (str,), {'creator':
'akilesh})
new_str.__mro__
Mixing up
class dancer(object):
def dance(self):
print 'I am dancing'
class singer(object):
def sing(self):
print 'I am singing'
class gunner(object):
def gun_me_down(self):
print 'you better run'
performer = type('performer', (dancer,
singer, gunner), {'name': 'akilesh'})
Mr_T = performer()
Mr_T.name
Mr_T.sing()
Mr_T.dance()
Mr_T.gun_me_down()
Thank You
Ageeleshwar K
akilesh1597@gmail.com

More Related Content

PPTX
Types of methods in python
PPTX
Chapter2 array of objects
PPTX
Object Oriented Programming_Lecture 2
ODP
06. haskell type builder
PPTX
‫Object Oriented Programming_Lecture 3
PPTX
PDF
Dive into Python Class
PPT
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات
Types of methods in python
Chapter2 array of objects
Object Oriented Programming_Lecture 2
06. haskell type builder
‫Object Oriented Programming_Lecture 3
Dive into Python Class
البرمجة الهدفية بلغة جافا - مصفوفة الكائنات

What's hot (12)

PPTX
Classes,object and methods jav
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
PDF
C,s&s
PDF
Metaprogramming in Ruby
DOCX
Keyword of java
PPTX
Inheritance Mixins & Traits
PDF
Review of c_sharp2_features_part_i
PDF
Reflection and Introspection
PPTX
‫‫Chapter4 Polymorphism
PPT
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Classes,object and methods jav
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
C,s&s
Metaprogramming in Ruby
Keyword of java
Inheritance Mixins & Traits
Review of c_sharp2_features_part_i
Reflection and Introspection
‫‫Chapter4 Polymorphism
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Ad

Similar to Python data modelling (20)

PPTX
classes and objects of python object oriented
PPTX
Advanced Python : Static and Class Methods
PPT
Introduction to Python - Part Three
PPTX
Object Oriented Programming.pptx
PDF
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
PPT
Reflection
PPT
Lesson on Python Classes by Matt Wufus 2003
PDF
Anton Kasyanov, Introduction to Python, Lecture5
PPTX
Ruby object model - Understanding of object play role for ruby
PPTX
Ruby object model
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
PPT
Lecture topic - Python class lecture.ppt
PPT
Lecture on Python class -lecture123456.ppt
PPTX
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PPT
Python3
PPTX
Module-5-Classes and Objects for Python Programming.pptx
PPTX
Object Oriented Programming in Python.pptx
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
classes and objects of python object oriented
Advanced Python : Static and Class Methods
Introduction to Python - Part Three
Object Oriented Programming.pptx
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Reflection
Lesson on Python Classes by Matt Wufus 2003
Anton Kasyanov, Introduction to Python, Lecture5
Ruby object model - Understanding of object play role for ruby
Ruby object model
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
Lecture topic - Python class lecture.ppt
Lecture on Python class -lecture123456.ppt
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
Python3
Module-5-Classes and Objects for Python Programming.pptx
Object Oriented Programming in Python.pptx
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Transform Your Business with a Software ERP System
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Nekopoi APK 2025 free lastest update
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
history of c programming in notes for students .pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Operating system designcfffgfgggggggvggggggggg
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Transform Your Business with a Software ERP System
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Understanding Forklifts - TECH EHS Solution
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Nekopoi APK 2025 free lastest update
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Navsoft: AI-Powered Business Solutions & Custom Software Development
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Odoo Companies in India – Driving Business Transformation.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
CHAPTER 2 - PM Management and IT Context
PTS Company Brochure 2025 (1).pdf.......
Upgrade and Innovation Strategies for SAP ERP Customers
history of c programming in notes for students .pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Operating system designcfffgfgggggggvggggggggg

Python data modelling

  • 1. Mixing it all up with Python
  • 2. We shall discuss ● Classes ● New style classes ● Inheritace heirarchy and MRO ● Mixins ● Meta Classes
  • 3. Classes ● Blueprints for creating an object ● Objects have properties(attributes) and actions(methods) ● Python Classes are a little more than that, but we shall see that later on ● Lets discuss on class definition
  • 4. Class class MyClass(BaseClass1, BaseClass2): def __init__(self, attr1, attr2): self.attr1 = attr1 self.attr2 = attr2 def do_something(self): Print 'doing something...'
  • 5. Stuff to discuss ● If there are no base classes you need not include the braces after the class name ● object initialization is done in __init__(constructor) ● first argument to the method is the object itself. Although it is customary to call the first argument 'self' it is not necessary ● The object has already been created by the time __init__ is called
  • 6. New style classes ● Introduced in python 2.2 ● Inherit from 'object' ● Yes 'object' is actually a class, which act as the base class of new style classes ● And yes in python a class is also an object ● Never mind... ● In python 3 all classes are new style even if you do not explicitly inherit 'object'
  • 7. New style class class MyClass(object): def __init__(self, attr1, attr2): super(MyClass, self).__init__() self.attr1 = attr1 self.attr2 = attr2 def do_something(self): Print 'doing something...'
  • 8. What is the benifit of new style classes ● type class unification ● you can now subclass list, dict etc and create a custom type that has all qualities of a builtin with some extra functionality ● Follow on for more benifits
  • 9. Old vs New class X: def __init__(self, value): self.value = value def __eq__(self, other): return self.value == other.value class X(object): def __init__(self, value): super(X, self).__init__() self.value = value def __eq__(self, other): return self.value == other.value
  • 10. Beginners better not ● If you want to alter the object creation you are looking for __new__() ● Unlike __init__, __new__ is a staticmethod that receives the class itself as its first argument. ● This is used to alter the object creation process itself ● Used only when you have to subclass imutables ● The __init__ method of the newly created object is invoked only if __new__ returns an object of the class's type
  • 11. example class X(str): def __new__(cls, arg1): return str.__new__(cls, arg1.upper()) def __init__(self, my_text): self.original_text = my_text
  • 12. Class Customization ● You can customize your classes with – __eq__, __ne__, __lt__, __gt__, __le__, __ge__, __cmp__, – __nonzero__ called when you do bool(yourObject) – If __nonzero__ does not exist __len__ is tried – If both do not exist all values are assumed to be possitive ● reference
  • 13. MRO ● Defines where the interpreter looks, in the inheritance chain, for a method when invoked on an object ● It is the same logic for attributes although it is named MRO ● Python 2.2 would do a depth first left to right
  • 14. C3 algorithm ● Introduced in 2.3 ● A MRO is bad when it breaks such fundamental properties as – local precedence ordering ● The order of base classes as defined by user – Monotonicity ● A MRO is monotonic when the following is true: if C1 precedes C2 in the linearization of C, then C1 precedes C2 in the linearization of any subclass of C ● 2.3 Will raise an TypeError when you define a bad MRO ● reference
  • 15. A Bad MRO class X(object): pass class Y(object): pass class A(X, Y): pass class B(Y, X): pass class C(A, B): pass
  • 16. C3 Algorithm take the head of the first list, if this head is not in the tail of any of the other lists, then add it to the linearization of C and remove it from the lists in the merge, otherwise look at the head of the next list and take it, if it is a good head. Then repeat the operation until all the class are removed or it is impossible to find good heads. In this case, it is impossible to construct the merge, Python 2.3 will refuse to create the class C and will raise an exception.
  • 17. Appy to our example MRO(A) = AXYO MRO(B) = BYXO MRO(C) = C , AXYO, BYXO, AB MRO(C) = C, A, XYO, BYXO, B MRO(C) = C, A, B, XYO, YXO Error- X appears in tail position in YXO and Y appears in tail position in XYO
  • 18. Mixins ● A form of inheritance where your bases implement a few methods – That can be mixed with out affecting the inheriting class, but adding more features – That may call methods that may not be defined in the same class ● Mixins are not classes that can be instantiated ● Used when implementing a logic involving optional features, extended features etc ● Not very usefull unless you want to create a class from a bunch of mixin classes at run time
  • 19. Meta Classes ● It is the class of a class ● Allows creation of class at run time ● Possible because in python everything is an object includng class and hence can be created and modified at run time ● 'type()' is not a function, its a metaclass ● And yes metaclass can be subclassed ● reference
  • 20. examples a = str('hello world') type(a) type(type(a)) str.__class__ new_str = type('new_string_type', (str,), {'creator': 'akilesh}) new_str.__mro__
  • 21. Mixing up class dancer(object): def dance(self): print 'I am dancing' class singer(object): def sing(self): print 'I am singing' class gunner(object): def gun_me_down(self): print 'you better run' performer = type('performer', (dancer, singer, gunner), {'name': 'akilesh'}) Mr_T = performer() Mr_T.name Mr_T.sing() Mr_T.dance() Mr_T.gun_me_down()