Slide 1

Slide 1 text

Dive into Object-Oriented Python Leonardo Giordani @lgiordani http://thedigitalcatonline.com

Slide 2

Slide 2 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani About Me Born in 1977 with Star Wars, bash, Apple ][, BSD, finger, Zork, Galaxy Express 999, Little Pollon, Dire Straits, The Police, Rumours, The Silmarillion, Squad Leader. Interested in operating systems and computer languages, photography, fantasy and science fiction, video- and boardgames, guitar playing, climbing, horseback riding, Aikido, rollerskating, drawing, painting, bookbinding. I programmed in Z80 and x86 Assembly, GW-Basic, Logo, Borland Turbo Pascal, Prolog, C, C++, PHP, Lisp, Ada, Objective-C, bash, Python, Erlang, Clojure, Scala, JavaScript. I love mathematics and cryptography. tl; dr me = nerd + coder

Slide 3

Slide 3 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The Digital Cat thedigitalcatonline.com @thedigicat @tw_lgiordani lgiordani LeonardoGiordani Get in touch

Slide 4

Slide 4 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani import users import talk assert all([a >= users.beginner for a in talk.attendees]) About You

Slide 5

Slide 5 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Objects Class and instance Delegation Polymorphism Overview

Slide 6

Slide 6 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Source Code Repository http://github.com/lgiordani/oopy

Slide 7

Slide 7 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Objects PART 1

Slide 8

Slide 8 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani data = (13, 63, 5, 378, 58, 40) def avg(d): return sum(d)/len(d) >>> avg(data) 92.83333333333333 Plain old procedures

Slide 9

Slide 9 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani door1 = [1, 'closed'] door2 = [2, 'closed'] def open_door(door): door[1] = 'open' >>> open_door(door1) >>> door1 [1, 'open'] Procedures can modify data

Slide 10

Slide 10 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani door1 = [1, 'closed'] door2 = [2, 'closed'] ldoor1 = [1, 'closed', 'unlocked'] def open_door(door): door[1] = 'open' def open_ldoor(door): if door[2] == 'unlocked': door[1] = 'open' Things can get complicated

Slide 11

Slide 11 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani door1 = [1, 'closed'] door2 = [2, 'closed'] ldoor1 = [1, 'closed', 'unlocked'] def open_door(door): door[1] = 'open' def open_ldoor(door): if door[2] == 'unlocked': door[1] = 'open' >>> open_door(door1) >>> door1 [1, 'open'] >>> open_ldoor(ldoor1) >>> ldoor1 [1, 'open', 'unlocked'] Things can get complicated

Slide 12

Slide 12 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The meaning of the word 'type' Behavioural meaning

Slide 13

Slide 13 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The meaning of the word 'type' Structural meaning

Slide 14

Slide 14 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The behavioural meaning is important

Slide 15

Slide 15 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani © 2000 Alex Martelli on comp.lang.python Duck typing: make it behave like a duck

Slide 16

Slide 16 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Duck typing joke #1

Slide 17

Slide 17 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Duck typing joke #2

Slide 18

Slide 18 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The meaning of the word 'class'

Slide 19

Slide 19 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani The meaning of the word 'instance'

Slide 20

Slide 20 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 6 >>> a 6 >>> type(a) >>> a = int(6) >>> a 6 >>> a.__class__ You already used classes

Slide 21

Slide 21 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani You already used classes >>> a = 6 >>> a 6 >>> type(a) >>> a = int(6) >>> a 6 >>> a.__class__ 2.x

Slide 22

Slide 22 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Find some types that Python provides out of the box. 2. Can you create a float variable without using the '.' character? (i.e. do not write a=5.4). 3. What is the difference between {1,2,3} and {'a':1, 'b':2}? How can you tell it?

Slide 23

Slide 23 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Find some types that Python provides out of the box. >>> type(4) >>> type(4.5) >>> type('some words') >>> type([1,2,3,4]) >>> type((1,2,3,4)) >>> type({1,2,3,4}) >>> type({'first name':'Ray', 'last name':'Stantz'}) >>> type(True)

Slide 24

Slide 24 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 2. Can you create a float variable without using the '.' character? (i.e. do not write a=5.4). >>> a = float(4) >>> type(a)

Slide 25

Slide 25 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 3. What is the difference between {1,2,3} and {'a':1, 'b':2}? How can you tell it? >>> type({1,2,3}) >>> type({'a':1, 'b':2})

Slide 26

Slide 26 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' The first class

Slide 27

Slide 27 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani class Door(object): def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' The first class 2.x

Slide 28

Slide 28 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' The first class

Slide 29

Slide 29 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') The first class class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 30

Slide 30 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> type(door1) The first class class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 31

Slide 31 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> type(door1) >>> door1.number 1 >>> door1.status 'closed' The first class class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 32

Slide 32 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> type(door1) >>> door1.number 1 >>> door1.status 'closed' >>> door1.open() The first class class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 33

Slide 33 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> type(door1) >>> door1.number 1 >>> door1.status 'closed' >>> door1.open() >>> door1.number 1 >>> door1.status 'open' The first class class Door: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 34

Slide 34 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Create a ColouredDoor class that has the colour attribute. 2. Create a ClosedDoor class that has a default status of 'closed'. 3. Create a ToggleDoor class that has a method toggle() that toggles the status of the door.

Slide 35

Slide 35 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Create a ColouredDoor class that has the colour attribute. class ColouredDoor: def __init__(self, number, status, colour): self.number = number self.status = status self.colour = colour def open(self): self.status = 'open' def close(self): self.status = 'closed' Exercises

Slide 36

Slide 36 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Create a ClosedDoor class that has a default status of 'closed'. class ClosedDoor: def __init__(self, number, status='closed'): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' Exercises

Slide 37

Slide 37 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 3. Create a ToggleDoor class that has a method toggle() that toggles the status of the door. class ToggleDoor: def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' def toggle(self): d = {'open': 'closed', 'closed': 'open'} self.status = d[self.status] Exercises

Slide 38

Slide 38 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class and instance PART 2

Slide 39

Slide 39 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Where is the class of an object? >>> door1 = Door(1, 'closed') >>> door2 = Door(1, 'closed')

Slide 40

Slide 40 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Where is the class of an object? >>> door1 = Door(1, 'closed') >>> door2 = Door(1, 'closed') >>> hex(id(door1)) '0xb67e148c' >>> hex(id(door2)) '0xb67e144c'

Slide 41

Slide 41 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Where is the class of an object? >>> door1 = Door(1, 'closed') >>> door2 = Door(1, 'closed') >>> hex(id(door1)) '0xb67e148c' >>> hex(id(door2)) '0xb67e144c' >>> hex(id(door1.__class__)) '0xb685f56c' >>> hex(id(door2.__class__)) '0xb685f56c'

Slide 42

Slide 42 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes class Door: colour = 'brown' def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 43

Slide 43 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> door1 = Door(1, 'closed') >>> door2 = Door(2, 'closed')

Slide 44

Slide 44 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> door1 = Door(1, 'closed') >>> door2 = Door(2, 'closed') >>> Door.colour 'brown' >>> door1.colour 'brown' >>> door2.colour 'brown'

Slide 45

Slide 45 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.colour = 'white'

Slide 46

Slide 46 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.colour = 'white' >>> Door.colour 'white' >>> door1.colour 'white' >>> door2.colour 'white'

Slide 47

Slide 47 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.colour = 'white' >>> Door.colour 'white' >>> door1.colour 'white' >>> door2.colour 'white' >>> hex(id(Door.colour)) '0xb67e1500' >>> hex(id(door1.colour)) '0xb67e1500' >>> hex(id(door2.colour)) '0xb67e1500'

Slide 48

Slide 48 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', '__dict__': , '__weakref__': , '__init__': , '__module__': '__main__', '__doc__': None, 'close': })

Slide 49

Slide 49 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', '__dict__': , '__weakref__': , '__init__': , '__module__': '__main__', '__doc__': None, 'close': }) >>> door1.__dict__ {'number': 1, 'status': 'closed'}

Slide 50

Slide 50 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', '__dict__': , '__weakref__': , '__init__': , '__module__': '__main__', '__doc__': None, 'close': }) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.__dict__['colour'] Traceback (most recent call last): File "", line 1, in KeyError: 'colour'

Slide 51

Slide 51 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class attributes >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', '__dict__': , '__weakref__': , '__init__': , '__module__': '__main__', '__doc__': None, 'close': }) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.__dict__['colour'] Traceback (most recent call last): File "", line 1, in KeyError: 'colour' >>> door1.__class__.__dict__['colour'] 'white' >>> door1.colour is Door.colour True

Slide 52

Slide 52 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Attribute resolution Door door1 __getattribute__() door1.colour Door.colour door2

Slide 53

Slide 53 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.colour = 'white' >>> door1.__dict__['colour'] 'white' >>> door1.__class__.__dict__['colour'] 'brown' Attribute resolution

Slide 54

Slide 54 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.colour = 'white' >>> door1.__dict__['colour'] 'white' >>> door1.__class__.__dict__['colour'] 'brown' >>> door1.colour 'white' >>> Door.colour 'brown' Attribute resolution

Slide 55

Slide 55 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.colour = 'white' >>> door1.__dict__['colour'] 'white' >>> door1.__class__.__dict__['colour'] 'brown' >>> door1.colour 'white' >>> Door.colour 'brown' >>> Door.colour = 'red' Attribute resolution

Slide 56

Slide 56 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.colour = 'white' >>> door1.__dict__['colour'] 'white' >>> door1.__class__.__dict__['colour'] 'brown' >>> door1.colour 'white' >>> Door.colour 'brown' >>> Door.colour = 'red' >>> door1.__dict__['colour'] 'white' >>> door1.__class__.__dict__['colour'] 'red' Attribute resolution

Slide 57

Slide 57 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Modify the Door class adding a class attribute 'status' with value 'undefined'. Does it work? What happens to instances? 2. Modify the Door class adding a class attribute 'status' with value 'closed' and remove status from __init__(). Does it work? 3. Add a toggle() method to the previous class. What happens if you call toggle() on a fresh instance? Why?

Slide 58

Slide 58 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Modify the Door class adding a class attribute 'status' with value 'undefined'. Does it work? What happens to instances? class Door: colour = 'brown' status = 'undefined' def __init__(self, number, status): self.number = number self.status = status def open(self): self.status = 'open' def close(self): self.status = 'closed' Exercises

Slide 59

Slide 59 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Modify the Door class adding a class attribute 'status' with value 'closed' and remove status from __init__(). Does it work? class Door: colour = 'brown' status = 'closed' def __init__(self, number): self.number = number def open(self): self.status = 'open' def close(self): self.status = 'closed' Exercises

Slide 60

Slide 60 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 3. Add a toggle() method to the previous class. What happens if you call toggle() on a fresh instance? Why? class Door: colour = 'brown' status = 'closed' def __init__(self, number): self.number = number def toggle(self): d = {'open': 'closed', 'closed': 'open'} self.status = d[self.status] Exercises

Slide 61

Slide 61 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', ...}) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.colour is Door.colour True Method resolution

Slide 62

Slide 62 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', ...}) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.colour is Door.colour True >>> door1.open is Door.open False Method resolution

Slide 63

Slide 63 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', ...}) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.colour is Door.colour True >>> door1.open is Door.open False >>> Door.__dict__['open'] >>> Door.open >>> door1.open > Method resolution

Slide 64

Slide 64 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.__dict__ mappingproxy({'open': , 'colour': 'white', ...}) >>> door1.__dict__ {'number': 1, 'status': 'closed'} >>> door1.colour is Door.colour True >>> door1.open is Door.open False >>> Door.__dict__['open'] >>> Door.open >>> door1.open > Method resolution 2.x

Slide 65

Slide 65 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class methods class Door: colour = 'brown' def __init__(self, number, status): self.number = number self.status = status @classmethod def knock(cls): print('Knock!') def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 66

Slide 66 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.knock() Knock! Class methods

Slide 67

Slide 67 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door1.knock() Knock! >>> Door.knock() Knock! Class methods

Slide 68

Slide 68 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Class methods class Door: colour = 'brown' def __init__(self, number, status): self.number = number self.status = status @classmethod def knock(cls): print('Knock!') @classmethod def paint(cls, colour): cls.colour = colour ...

Slide 69

Slide 69 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door2 = Door(2, 'closed') Class methods

Slide 70

Slide 70 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door1 = Door(1, 'closed') >>> door2 = Door(2, 'closed') >>> Door.colour 'brown' >>> door1.colour 'brown' >>> door2.colour 'brown' Class methods

Slide 71

Slide 71 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.paint('white') >>> Door.colour 'white' >>> door1.colour 'white' >>> door2.colour 'white' Class methods

Slide 72

Slide 72 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> Door.paint('white') >>> Door.colour 'white' >>> door1.colour 'white' >>> door2.colour 'white' >>> door1.paint('yellow') >>> Door.colour 'yellow' >>> door1.colour 'yellow' >>> door2.colour 'yellow' Class methods

Slide 73

Slide 73 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Modify the Door class adding a class method 'paint' that accepts a 'colour' arg and changes the class attribute 'colour'. 2. Modify the Door class adding both a class method 'paint' and a standard method 'paint'. What happens?

Slide 74

Slide 74 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Modify the Door class adding a class method 'paint' that accepts a 'colour' arg and changes the class attribute 'colour'. class Door: colour = 'brown' def __init__(self, number, status): self.number = number self.status = status @classmethod def paint(cls, colour): cls.colour = colour def paint(self, colour): self.colour = colour [...] Exercises

Slide 75

Slide 75 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Modify the Door class adding both a class method 'paint' and a standard method 'paint'. What happens? Exercises class Door: colour = 'brown' def __init__(self, number, status): self.number = number self.status = status @classmethod def open(cls, colour): cls.colour = colour def open(self): self.status = 'open' def close(self): self.status = 'closed'

Slide 76

Slide 76 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Delegation PART 3

Slide 77

Slide 77 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Specialization Cat Animal

Slide 78

Slide 78 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Specialization Cat Animal Cat has all the features of Animal, i.e. 'moves'

Slide 79

Slide 79 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Specialization Cat Animal Cat can provide new features, i.e. 'has whiskers'

Slide 80

Slide 80 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Specialization Cat Animal Cat performs some or all the tasks performed by Animal in a different way, i.e. 'moves silently'

Slide 81

Slide 81 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Delegation Cat Animal Cat implements only 'new' or 'changed' features and delegates the remaining features to Animal

Slide 82

Slide 82 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Composition: 'to have' Car Engine Wheels turn_on() get_color() steer() turn_on() steer()

Slide 83

Slide 83 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance: 'to be' Cat move() mew() hiss() Animal move()

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance class SecurityDoor(Door): pass

Slide 86

Slide 86 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance class SecurityDoor(Door): pass >>> sdoor = SecurityDoor(1, 'closed')

Slide 87

Slide 87 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance class SecurityDoor(Door): pass >>> sdoor = SecurityDoor(1, 'closed') >>> SecurityDoor.colour is Door.colour True >>> sdoor.colour is Door.colour True

Slide 88

Slide 88 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance class SecurityDoor(Door): pass >>> sdoor = SecurityDoor(1, 'closed') >>> SecurityDoor.colour is Door.colour True >>> sdoor.colour is Door.colour True sdoor.colour SecurityDoor.colour Door.colour

Slide 89

Slide 89 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance >>> sdoor.__dict__ {'number': 1, 'status': 'closed'}

Slide 90

Slide 90 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance >>> sdoor.__dict__ {'number': 1, 'status': 'closed'} >>> sdoor.__class__.__dict__ mappingproxy({'__doc__': None, '__module__': '__main__'})

Slide 91

Slide 91 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Inheritance >>> sdoor.__dict__ {'number': 1, 'status': 'closed'} >>> sdoor.__class__.__dict__ mappingproxy({'__doc__': None, '__module__': '__main__'}) >>> Door.__dict__ mappingproxy({'__dict__': , 'colour': 'yellow', 'open': , '__init__': , '__doc__': None, 'close': , 'knock': , '__weakref__': , '__module__': '__main__', 'paint': })

Slide 92

Slide 92 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> sdoor.__dict__ {'number': 1, 'status': 'closed'} >>> sdoor.__class__.__dict__ mappingproxy({'__doc__': None, '__module__': '__main__'}) >>> Door.__dict__ mappingproxy({'__dict__': , 'colour': 'yellow', 'open': , '__init__': , '__doc__': None, 'close': , 'knock': , '__weakref__': , '__module__': '__main__', 'paint': }) >>> SecurityDoor.__bases__ (,) Inheritance

Slide 93

Slide 93 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> sdoor.__dict__ {'number': 1, 'status': 'closed'} >>> sdoor.__class__.__dict__ mappingproxy({'__doc__': None, '__module__': '__main__'}) >>> Door.__dict__ mappingproxy({'__dict__': , 'colour': 'yellow', 'open': , '__init__': , '__doc__': None, 'close': , 'knock': , '__weakref__': , '__module__': '__main__', 'paint': }) >>> SecurityDoor.__bases__ (,) >>> sdoor.knock > Inheritance

Slide 94

Slide 94 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if not self.locked: self.status = 'open'

Slide 95

Slide 95 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if not self.locked: self.status = 'open' >>> SecurityDoor.__dict__ mappingproxy({'__doc__': None, '__module__': '__main__', 'open': , 'colour': 'grey', 'locked': True})

Slide 96

Slide 96 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Modify the SecurityDoor class adding a custom close_and_lock() method that changes status to 'closed' and locked to True. Test it. 2. Modify the SecurityDoor class adding a custom close() method that accepts a locked flag (with default). 3. Modify the SecurityDoor class adding a custom __init__() method that sets self.locked.

Slide 97

Slide 97 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Modify the SecurityDoor class adding a custom close_and_lock() method that changes status to 'closed' and locked to True. Test it. class SecurityDoor(Door): colour = 'grey' locked = False def open(self): if not self.locked: self.status = 'open' def close_and_lock(self): self.status = 'closed' self.locked = True Exercises

Slide 98

Slide 98 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Modify the SecurityDoor class adding a custom close() method that accepts a locked flag (with default). class SecurityDoor(Door): colour = 'grey' locked = False def open(self): if not self.locked: self.status = 'open' def close(self, locked=False): self.status = 'closed' self.locked = locked Exercises

Slide 99

Slide 99 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 3. Modify the SecurityDoor class adding a custom __init__() method that sets self.locked. class SecurityDoor(Door): colour = 'grey' def __init__(self, number, status, locked=False): self.number = number self.status = status self.locked = locked def open(self): if not self.locked: self.status = 'open' Exercises

Slide 100

Slide 100 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return Door.open(self)

Slide 101

Slide 101 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return Door.open(self) >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status 'closed'

Slide 102

Slide 102 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return Door.open(self) >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status 'closed' >>> sdoor.open() >>> sdoor.status 'closed'

Slide 103

Slide 103 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Overriding class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return Door.open(self) >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status 'closed' >>> sdoor.open() >>> sdoor.status 'closed' >>> sdoor.locked = False >>> sdoor.open() >>> sdoor.status 'open'

Slide 104

Slide 104 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Avoid strong coupling class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return super().open()

Slide 105

Slide 105 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Avoid strong coupling class SecurityDoor(Door): colour = 'grey' locked = True def open(self): if self.locked: return super(SecurityDoor, self).open() 2.x

Slide 106

Slide 106 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Composition class SecurityDoor: colour = 'grey' locked = True def __init__(self, number, status): self.door = Door(number, status) def open(self): if self.locked: return self.door.open() def close(self): self.door.close()

Slide 107

Slide 107 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Composition class SecurityDoor: colour = 'grey' locked = True def __init__(self, number, status): self.door = Door(number, status) def open(self): if self.locked: return self.door.open() def close(self): self.door.close() >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status Traceback (most recent call last): File "", line 1, in AttributeError: 'SecurityDoor' object has no attribute 'status'

Slide 108

Slide 108 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Composition class SecurityDoor: colour = 'grey' locked = True def __init__(self, number, status): self.door = Door(number, status) def open(self): if self.locked: return self.door.open() def close(self): self.door.close() def get_status(self): return self.door.status status = property(get_status) >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status 'closed'

Slide 109

Slide 109 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Python magic to the rescue class SecurityDoor: colour = 'grey' locked = True def __init__(self, number, status): self.door = Door(number, status) def open(self): if self.locked: return self.door.open() # def close(self): # self.door.close() def __getattr__(self, attr): return getattr(self.door, attr) >>> sdoor = SecurityDoor(1, 'closed') >>> sdoor.status 'closed'

Slide 110

Slide 110 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Composed inheritance? class ComposedDoor: def __init__(self, number, status): self.door = Door(number, status) def __getattr__(self, attr): return getattr(self.door, attr)

Slide 111

Slide 111 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Modify the SecurityDoor class adding a custom close_and_lock() method. Use super(). Try to implement it with composition. 2. Modify the SecurityDoor class adding a custom close() method that accepts a locked flag (with default). Use super(). Try to implement it with composition.

Slide 112

Slide 112 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Modify the SecurityDoor class adding a custom close_and_lock() method. Use super(). Try to implement it with composition. class SecurityDoor(Door): #inheritance colour = 'grey' def __init__(self, number, status, locked=False): super().__init__(number, status) self.locked = locked def open(self): if self.locked: return super().open() def close_and_lock(self): super().close() self.locked = True Exercises

Slide 113

Slide 113 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani class SecurityDoor: #composition colour = 'grey' def __init__(self, number, status, locked=False): self.door = Door(number, status) self.locked = locked def open(self): if self.locked: return self.door.open() def __getattr__(self, attr): return getattr(self.door, attr) def close_and_lock(self): self.door.close() self.locked = True Exercises

Slide 114

Slide 114 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Modify the SecurityDoor class adding a custom close() method that accepts a locked flag (with default). Use super(). Try to implement it with composition. class SecurityDoor(Door): #inheritance colour = 'grey' def __init__(self, number, status, locked=False): super().__init__(number, status) self.locked = locked def open(self): if self.locked: return super().open() def close(self, locked=False): super().close() self.locked = locked Exercises

Slide 115

Slide 115 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani class SecurityDoor: #composition colour = 'grey' def __init__(self, number, status, locked=False): self.door = Door(number, status) self.locked = locked def open(self): if self.locked: return self.door.open() def __getattr__(self, attr): return getattr(self.door, attr) def close(self, locked=False): self.door.close() self.locked = locked Exercises

Slide 116

Slide 116 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Slide 117

Slide 117 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) [..., '__subclasshook__', 'append', 'clear', ...]

Slide 118

Slide 118 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) [..., '__subclasshook__', 'append', 'clear', ...] >>> l.append

Slide 119

Slide 119 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) [..., '__subclasshook__', 'append', 'clear', ...] >>> l.append >>> a = l.append >>> a

Slide 120

Slide 120 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) [..., '__subclasshook__', 'append', 'clear', ...] >>> l.append >>> a = l.append >>> a >>> b = getattr(l, 'append') >>> b

Slide 121

Slide 121 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani getattr() >>> l = [1,2,3] >>> dir(l) [..., '__subclasshook__', 'append', 'clear', ...] >>> l.append >>> a = l.append >>> a >>> b = getattr(l, 'append') >>> b >>> a == b True >>> a is b False

Slide 122

Slide 122 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism PART 4

Slide 123

Slide 123 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 References

Slide 124

Slide 124 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 >>> type(a) >>> hex(id(a)) '0x83fe540' References

Slide 125

Slide 125 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 >>> type(a) >>> hex(id(a)) '0x83fe540' >>> a = 'five' >>> a 'five' References

Slide 126

Slide 126 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 >>> type(a) >>> hex(id(a)) '0x83fe540' >>> a = 'five' >>> a 'five' >>> type(a) >>> hex(id(a)) '0xb70d6560' References

Slide 127

Slide 127 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 >>> type(a) >>> hex(id(a)) '0x83fe540' >>> a = 'five' >>> a 'five' >>> type(a) >>> hex(id(a)) '0xb70d6560' References Strong type system: every variable has a type

Slide 128

Slide 128 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> a = 5 >>> a 5 >>> type(a) >>> hex(id(a)) '0x83fe540' >>> a = 'five' >>> a 'five' >>> type(a) >>> hex(id(a)) '0xb70d6560' References Dynamic type system: the type changes with the content

Slide 129

Slide 129 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani def echo(a): return a Variables are references

Slide 130

Slide 130 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani def echo(a): return a >>> echo(5) 5 >>> echo('five') 'five' Variables are references

Slide 131

Slide 131 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> 5 + 6 11

Slide 132

Slide 132 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> 5 + 6 11 >>> 5.5 + 6.6 12.1

Slide 133

Slide 133 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> 5 + 6 11 >>> 5.5 + 6.6 12.1 >>> "just a" + " string" 'just a string'

Slide 134

Slide 134 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> 5 + 6 11 >>> 5.5 + 6.6 12.1 >>> "just a" + " string" 'just a string' >>> [1,2,3] + [4,5,6] [1, 2, 3, 4, 5, 6] >>> (1,2,3) + (4,5,6) (1, 2, 3, 4, 5, 6)

Slide 135

Slide 135 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> 5 + 6 11 >>> 5.5 + 6.6 12.1 >>> "just a" + " string" 'just a string' >>> [1,2,3] + [4,5,6] [1, 2, 3, 4, 5, 6] >>> (1,2,3) + (4,5,6) (1, 2, 3, 4, 5, 6) >>> {'a':4, 'b':5} + {'c':7} Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

Slide 136

Slide 136 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> s = "Just a sentence" >>> len(s) 15 >>> l = [1, 2, 3] >>> len(l) 3 >>> d = {'a': 1, 'b': 2} >>> len(d) 2

Slide 137

Slide 137 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> s = "Just a sentence" >>> len(s) 15 >>> l = [1, 2, 3] >>> len(l) 3 >>> d = {'a': 1, 'b': 2} >>> len(d) 2 >>> i = 5 >>> len(i) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'int' has no len()

Slide 138

Slide 138 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> s.__len__() 15 >>> l.__len__() 3 >>> d.__len__() 2

Slide 139

Slide 139 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani What is polymorphism? >>> s.__len__() 15 >>> l.__len__() 3 >>> d.__len__() 2 >>> i.__len__() Traceback (most recent call last): File "", line 1, in AttributeError: 'int' object has no attribute '__len__'

Slide 140

Slide 140 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation >>> [1,2,3].__add__([4,5,6]) [1, 2, 3, 4, 5, 6]

Slide 141

Slide 141 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation >>> [1,2,3].__add__([4,5,6]) [1, 2, 3, 4, 5, 6] >>> dir([1,2,3]) ['__add__', '__class__', '__contains__', ...]

Slide 142

Slide 142 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation >>> [1,2,3].__add__([4,5,6]) [1, 2, 3, 4, 5, 6] >>> dir([1,2,3]) ['__add__', '__class__', '__contains__', ...] >>> 1 in [1,2,3] True >>> [1,2,3].__contains__(1) True

Slide 143

Slide 143 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation >>> [1,2,3].__add__([4,5,6]) [1, 2, 3, 4, 5, 6] >>> dir([1,2,3]) ['__add__', '__class__', '__contains__', ...] >>> 1 in [1,2,3] True >>> [1,2,3].__contains__(1) True >>> 6 in [1,2,3] False >>> [1,2,3].__contains__(6) False

Slide 144

Slide 144 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation def sum(a, b): return a + b

Slide 145

Slide 145 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation def sum(a, b): return a + b >>> sum(5,6) 11 >>> sum("Being ", "polymorphic") 'Being polymorphic' >>> sum([1,2,3], [4,5,6]) [1, 2, 3, 4, 5, 6]

Slide 146

Slide 146 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism is based on delegation def sum(a, b): return a + b >>> sum(5,6) 11 >>> sum("Being ", "polymorphic") 'Being polymorphic' >>> sum([1,2,3], [4,5,6]) [1, 2, 3, 4, 5, 6] >>> sum([1,2,3], 8) Traceback (most recent call last): File "", line 1, in File "", line 2, in sum TypeError: can only concatenate list (not "int") to list

Slide 147

Slide 147 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Create a class that contains an integer as self.value and with a __len__() method that returns the number of digits of the integer. Does len() work for instances of this class? 2. Add a __contains__() method that returns True if self.value contains the given digit. Does 'in' work for this type? 3. Try str() on an instance of your class. What happens? How can you return a better string representation (e.g. to show the actual value)?

Slide 148

Slide 148 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Create a class that contains an integer as self.value and with a __len__() method that returns the number of digits of the integer. Does len() work for instances of this class? class CustomInteger: def __init__(self, value): self.value = value def __len__(self): return len(str(self.value)) Exercises

Slide 149

Slide 149 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Add a __contains__() method that returns True if self.value contains the given digit. Does 'in' work for this type? class CustomInteger: def __init__(self, value): self.value = value def __len__(self): return len(str(self.value)) def __contains__(self, digit): return str(digit) in str(self.value) Exercises

Slide 150

Slide 150 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 3. Try str() on an instance of your class. What happens? How can you return a better string representation (e.g. to show the actual value)? class CustomInteger: def __init__(self, value): self.value = value def __len__(self): return len(str(self.value)) def __contains__(self, digit): return str(digit) in str(self.value) def __str__(self): return super().__str__() + ' [{}]'.format(self.value) Exercises

Slide 151

Slide 151 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism in action class Room: def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open()

Slide 152

Slide 152 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism in action class Door: def __init__(self): self.status = "closed" def open(self): self.status = "open" def close(self): self.status = "closed" def is_open(self): return self.status == "open"

Slide 153

Slide 153 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Polymorphism in action class BooleanDoor: def __init__(self): self.status = True def open(self): self.status = True def close(self): self.status = False def is_open(self): return self.status

Slide 154

Slide 154 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door = Door() >>> bool_door = BooleanDoor() Polymorphism in action

Slide 155

Slide 155 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door = Door() >>> bool_door = BooleanDoor() >>> room = Room(door) >>> bool_room = Room(bool_door) Polymorphism in action

Slide 156

Slide 156 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door = Door() >>> bool_door = BooleanDoor() >>> room = Room(door) >>> bool_room = Room(bool_door) >>> room.open() >>> room.is_open() True >>> room.close() >>> room.is_open() False Polymorphism in action

Slide 157

Slide 157 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani >>> door = Door() >>> bool_door = BooleanDoor() >>> room = Room(door) >>> bool_room = Room(bool_door) >>> room.open() >>> room.is_open() True >>> room.close() >>> room.is_open() False >>> bool_room.open() >>> bool_room.is_open() True >>> bool_room.close() >>> bool_room.is_open() False Polymorphism in action

Slide 158

Slide 158 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Exercises 1. Modify the Room, the Door and the BooleanDoor classes to add a toggle() method that changes open status to closed and vice versa. 2. Change the Room class such that it accepts a class instead of an instance. How do you manage it into __init__()? Does it still work? 3. Create a PetDoor object that inherits from Door (or BooleanDoor). Add the suitable methods to manage the small pet door. Does the Room work? 4. May you reuse the Door to create the pet door?

Slide 159

Slide 159 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 1. Modify the Room, the Door and the BooleanDoor classes to add a toggle() method that changes open status to closed and vice versa. class Room: def __init__(self, door): self.door = door def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() def toggle(self): if self.door.is_open(): self.door.close() else: self.door.open() Exercises

Slide 160

Slide 160 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 2. Change the Room class such that it accepts a class instead of an instance. How do you manage it into __init__()? Does it still work? class Room: def __init__(self, door_cls): self.door = door_cls() def open(self): self.door.open() def close(self): self.door.close() def is_open(self): return self.door.is_open() >>> room = Room(Door) >>> bool_room = Room(BooleanDoor) Exercises

Slide 161

Slide 161 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 3. Create a PetDoor object that inherits from Door (or BooleanDoor). Add the suitable methods to manage the small pet door. Does the Room work? class PetDoor(BooleanDoor): def __init__(self): super().__init__() self.pet_door = 'open' def open_pet_door(self): self.pet_door = 'open' def close_pet_door(self): self.pet_door = 'closed' Exercises

Slide 162

Slide 162 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani 4. May you reuse the Door to create the pet door? class PetDoor(BooleanDoor): def __init__(self): super().__init__() self.pet_door = BooleanDoor() def open_pet_door(self): self.pet_door.open() def close_pet_door(self): self.pet_door.close() Exercises

Slide 163

Slide 163 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani if hasattr(someobj, 'open'): [...] else: [...] “Ask for permission” style

Slide 164

Slide 164 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani try: someobj.open() [...] except AttributeError: [...] “Ask for forgiveness” style

Slide 165

Slide 165 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Cast Font awesome icons by Freepik Guido van Rossum and the Python core developers Open source programmers, stackoverflow and forum contributors, blog authors, convention and meetup attendees Many hours spent studying and experimenting

Slide 166

Slide 166 text

Dive into Object-Oriented Python – Leonardo Giordani - @lgiordani Cast thedigitalcatonline.com Interested in AMQP, C, Clojure, concurrent programming, C++, decorators, Django, Erlang, functional programming, generators, Git, metaclasses, metaprogramming, Notebook, OOP, operating systems, Python, Qt, RabbitMQ, Scala, TDD, versioning? @thedigicat

Slide 167

Slide 167 text

Dive into Object-Oriented Python http://thedigitalcatonline.com Leonardo Giordani @lgiordani