python data modelling

22
Mixing it all up with Python

Upload: ageeleshwar-k

Post on 15-Jul-2015

290 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Python data modelling

Mixing it all up with Python

Page 2: Python data modelling

We shall discuss

● Classes● New style classes● Inheritace heirarchy and MRO● Mixins● Meta Classes

Page 3: Python data modelling

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

Page 4: Python data modelling

Class

class MyClass(BaseClass1, BaseClass2):

def __init__(self, attr1, attr2):

self.attr1 = attr1

self.attr2 = attr2

def do_something(self):

Print 'doing something...'

Page 5: Python data modelling

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

Page 6: Python data modelling

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'

Page 7: Python data modelling

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...'

Page 8: Python data modelling

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

Page 9: Python data modelling

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

Page 10: Python data modelling

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

Page 11: Python data modelling

example

class X(str):

def __new__(cls, arg1):

return str.__new__(cls, arg1.upper())

def __init__(self, my_text):

self.original_text = my_text

Page 12: Python data modelling

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

Page 13: Python data modelling

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

Page 14: Python data modelling

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

Page 15: Python data modelling

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

Page 16: Python data modelling

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.

Page 17: Python data modelling

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

Page 18: Python data modelling

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

Page 19: Python data modelling

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

Page 20: Python data modelling

examples

a = str('hello world')

type(a)

type(type(a))

str.__class__

new_str = type('new_string_type', (str,), {'creator': 'akilesh})

new_str.__mro__

Page 21: Python data modelling

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()

Page 22: Python data modelling

Thank YouAgeeleshwar K

[email protected]