Presentation is loading. Please wait.

Presentation is loading. Please wait.

Act I: Exposition where we meet our characters and the world they live in.

Similar presentations


Presentation on theme: "Act I: Exposition where we meet our characters and the world they live in."— Presentation transcript:

1

2 Act I: Exposition where we meet our characters and the world they live in

3

4 Composability

5 Compositionality

6 Unix pipesUnix pipes $ ps aux | grep celery | grep -v grep | awk '{print $2}' | xargs kill -9

7 Unix pipesUnix pipes $ ps aux | grep celery | grep -v grep | awk '{print $2}' | xargs kill -9

8 Unix pipesUnix pipes $ command1 | command2 | command3 |...

9 Godtfred Kirk ChristiansenGodtfred Kirk Christiansen

10

11 Joe ArmstrongJoe Armstrong

12 grep REGEX matches

13 Quality

14

15 Robert M. PirsigRobert M. Pirsig

16 Jamie ZawinskiJamie Zawinski

17 Quality Composability Compositionality

18 Act II: Rising Action where we learn how inheritance in Python works

19 If you use old-style classes Youre gonna have a bad time

20 Method Resolution OrderMethod Resolution Order >>> class A(object):... pass... >>> A.mro() [, ]

21 Method Resolution OrderMethod Resolution Order object A

22 Method Resolution OrderMethod Resolution Order >>> class A(object): pass... >>> class B(object): pass... >>> class AB(A, B): pass...

23 The Diamond ProblemThe Diamond Problem object AB AB

24 The Diamond ProblemThe Diamond Problem object AB AB

25 The Diamond ProblemThe Diamond Problem >>> class A(object): pass... >>> class B(object): pass... >>> class AB(A, B): pass... >>> AB.mro() [,,, ]

26 Method Resolution OrderMethod Resolution Order >>> class A(object):... def say(self, what):... return what + 'a... >>> class B(object):... def say(self, what):... return what + 'b... >>> class AB(A, B): pass... >>> class BA(B, A): pass... >>> AB().say('hello:') 'hello:a' >>> BA().say('hey:') 'hey:b'

27 Cooperative InheritanceCooperative Inheritance class A(object): def __init__(self, arg_a): self.arg_a = arg_a class B(object): def __init__(self, arg_b): self.arg_b = arg_b class AB(A, B): def __init__(self, arg_a, arg_b): # ???

28 Cooperative InheritanceCooperative Inheritance class A(object): def __init__(self, arg_a): self.arg_a = arg_a class B(object): def __init__(self, arg_b): self.arg_b = arg_b class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b)

29 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b) >>> ab = AB('a', 'b') >>> ab.arg_a 'a' >>> ab.arg_b 'b'

30 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b) class C(D, AB): def __init__(self, arg_c): D.__init__(self) AB.__init__(self, *arg_c.split('.', 1)) >>> c=C('1.0') >>> c.arg_a '1' >>> c.arg_b '0

31 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b) class C(D, AB): def __init__(self, arg_c): D.__init__(self) AB.__init__(self, *arg_c.split('.', 1)) >>> C.mro() [,,,,, ]

32 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b) class D(A): def __init__(self): A.__init__(self, 'd') class C(D, AB): def __init__(self, arg_c): D.__init__(self) AB.__init__(self, *arg_c.split('.', 1))

33 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): A.__init__(self, arg_a) B.__init__(self, arg_b) class C(D, AB): def __init__(self, arg_c): D.__init__(self) AB.__init__(self, *arg_c.split('.', 1)) >>> C.mro() [,,,,, ] A.__init__(self, arg_a) super(AB, self).__init__(arg_a)

34 Cooperative InheritanceCooperative Inheritance class AB(A, B): def __init__(self, arg_a, arg_b): super(AB, self).__init__(arg_a=arg_a, arg_b=arg_b) class D(A): def __init__(self): super(D, self).__init__(arg_a='d') class C(D, AB): def __init__(self, arg_c): super(C, self).__init__( *arg_c.split('.', 1))

35 Cooperative InheritanceCooperative Inheritance class D(A): def __init__(self): super(D, self).__init__(arg_a='d') class C(D, AB): def __init__(self, arg_c): super(C, self).__init__( *arg_c.split('.', 1)) >>> C('1.0') Traceback (most recent call last): File " ", line 1, in File "mrosuper.py", line 27, in __init__ super(C, self).__init__(*arg_c.split('.', 1)) TypeError: __init__() takes exactly 1 argument (3 given)

36 Cooperative InheritanceCooperative Inheritance class D(A): def __init__(self): super(D, self).__init__(arg_a='d') class C(D, AB): def __init__(self, arg_c): super(C, self).__init__( *arg_c.split('.', 1)) >>> C('1.0') Traceback (most recent call last): File " ", line 1, in File "mrosuper.py", line 27, in __init__ super(C, self).__init__(*arg_c.split('.', 1)) TypeError: __init__() takes exactly 1 argument (3 given)

37 class A(object): def __init__(self, arg_a, **kwargs): self.arg_a = arg_a super(A, self).__init__(**kwargs) class B(object): def __init__(self, arg_b, **kwargs): self.arg_b = arg_b super(B, self).__init__(**kwargs) class AB(A, B): def __init__(self, arg_a, arg_b, **kwargs): super(AB, self).__init__(arg_a=arg_a, arg_b=arg_b, **kwargs) class D(A): def __init__(self, **kwargs): super(D, self).__init__(arg_a='d', **kwargs) class C(D, AB): def __init__(self, arg_c, **kwargs): super(C, self).__init__(*arg_c.split('.', 1), **kwargs)

38 class A(object): def __init__(self, arg_a, **kwargs): self.arg_a = arg_a super(A, self).__init__(**kwargs) class B(object): def __init__(self, arg_b, **kwargs): self.arg_b = arg_b super(B, self).__init__(**kwargs) class AB(A, B): def __init__(self, arg_a, arg_b, **kwargs): super(AB, self).__init__(arg_a=arg_a, arg_b=arg_b, **kwargs) class D(A): def __init__(self, **kwargs): super(D, self).__init__(arg_a='d', **kwargs) class C(D, AB): def __init__(self, arg_c, **kwargs): super(C, self).__init__(*arg_c.split('.', 1), **kwargs)

39 class A(object): def __init__(self, arg_a, **kwargs): self.arg_a = arg_a super(A, self).__init__(**kwargs) class B(object): def __init__(self, arg_b, **kwargs): self.arg_b = arg_b super(B, self).__init__(**kwargs) class AB(A, B): def __init__(self, arg_a, arg_b, **kwargs): super(AB, self).__init__(arg_a=arg_a, arg_b=arg_b, **kwargs) class D(A): def __init__(self, **kwargs): super(D, self).__init__(arg_a='d', **kwargs) class C(D, AB): def __init__(self, arg_c, **kwargs): super(C, self).__init__(*arg_c.split('.', 1), **kwargs)

40 THIS SHIT IS HARD

41 class A(object): def __init__(self, arg_a, **kwargs): self.arg_a = arg_a super(A, self).__init__(**kwargs) class B(object): def __init__(self, arg_b, **kwargs): self.arg_b = arg_b super(B, self).__init__(**kwargs) class AB(A, B): def __init__(self, arg_a, arg_b, **kwargs): kwargs['arg_a'], kwargs['arg_b'] = arg_a, arg_b super(AB, self).__init__(**kwargs) class D(A): def __init__(self, **kwargs): kwargs['arg_a'] = 'd' super(D, self).__init__(**kwargs) class C(D, AB): def __init__(self, arg_c, **kwargs): kwargs['arg_a'], kwargs['arg_b'] = arg_c.split('.', 1) super(C, self).__init__(**kwargs)

42 Its still different!Its still different! >>> c=C('1.0') >>> c.arg_a u'd' >>> c.arg_b u'0'

43 Cooperative InheritanceCooperative Inheritance Dont omit super(C, self).__init__() even if your base class is object Dont assume you know what arguments youre going to get Dont assume you know what arguments you should pass to super always pass all arguments you received on to super if classes can take differing arguments, always accept **kwargs

44 If you mix Class.__init__ and super() Youre gonna have a bad time

45 Mixins Not meant for instantiation on their own Enhance classes with independent functionality Not a form of specialisation but collection of functionality Like interfaces with built-in implementation Very reusable if orthogonal to the main type

46 Interlude Django ORM inheritance model sucks

47 The Diamond ProblemThe Diamond Problem M1 M2M3 M4

48 Polymorphism M1 M2M3M4

49 Liskov substitution principleLiskov substitution principle M1 M2M3M4

50 Act III: The Climax

51 Yep, here comes the demo.

52 And by the wayAnd by the way Just so you know.

53

54

55 Jamie ZawinskiJamie Zawinski

56


Download ppt "Act I: Exposition where we meet our characters and the world they live in."

Similar presentations


Ads by Google