A short tour into the python data modelling, dealing with classes, type-class unification, method resolution order in multiple inheritance, mixins and metaclasses
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
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
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