Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Similar presentations


Presentation on theme: "Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See"— Presentation transcript:

1 Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Classes and Objects

2 Inheritance Interpolating time series signals time strength

3 Classes and ObjectsInheritance Interpolating time series signals time strength step function linear interpolation

4 Classes and ObjectsInheritance Interpolating time series signals time strength step function linear interpolation A lot of the implementation is the same

5 Classes and ObjectsInheritance Interpolating time series signals time strength step function linear interpolation A lot of the implementation is the same How can we eliminate the reduncancy?

6 Classes and ObjectsInheritance First implementation class StepSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 raise IndexError, '%f too high' % where

7 Classes and ObjectsInheritance Second implementation class LinearSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 + (y1-y0) * (where-x0) / (x1-x0) raise IndexError, '%f too high' % where

8 Classes and ObjectsInheritance Second implementation class LinearSignal(object):... def get(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return y0 + (y1-y0) * (where-x0) / (x1-x0) raise IndexError, '%f too high' % where

9 Classes and ObjectsInheritance Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] class LinearSignal(object): def get(self, where): i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0)

10 Classes and ObjectsInheritance Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] class LinearSignal(object): def get(self, where): i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0) Where to put find?

11 Classes and ObjectsInheritance Refactor class StepSignal(object): def get(self, where): i = self.find(self, where) return self.values[i][1] class LinearSignal(object): def get(self, where): i = self.find(self, where) x0, y0 = self.values[i] x1, y1 = self.values[i+1] return y0 + (y1-y0) * (where-x0)/(x1-x0) Where to put find? Don't want to duplicate

12 Classes and ObjectsInheritance Use inheritance

13 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello'

14 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye'

15 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent

16 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' c = Child() c.goodbye() goodbye Child inherits from Parent

17 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' c = Child() c.goodbye() goodbye c.hello() hello Child inherits from Parent

18 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' c = Child() c.goodbye() goodbye c.hello() hello p = Parent() p.hello() hello Child inherits from Parent

19 Classes and ObjectsInheritance Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' c = Child() c.goodbye() goodbye c.hello() hello p = Parent() p.hello() hello p.goodbye() AttributeError: 'Parent' object has no attribute 'goodbye' Child inherits from Parent

20 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

21 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

22 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

23 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

24 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

25 Classes and ObjectsInheritance Contents of memory p c Parent object Child hello goodbye

26 Classes and ObjectsInheritance class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where

27 Classes and ObjectsInheritance class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where Not much use on its own

28 Classes and ObjectsInheritance class InterpolatedSignal(object): def find(self, where): if where < self.values[0][0]: raise IndexError, '%f too low' % where for i in range(len(self.values)-1): x0, y0 = self.values[i] x1, y1 = self.values[i+1] if x0 <= where <= x1: return i raise IndexError, '%f too high' % where Not much use on its own Where does this come from?

29 Classes and ObjectsInheritance Derive specific interpolators

30 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

31 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

32 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

33 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1]

34 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1] Fragile

35 Classes and ObjectsInheritance Derive specific interpolators class StepSignal(InterpolatedSignal): def __init__(self, values): self.values = values[:] def get(self, where): i = self.find(where) return self.values[i][1] Dependencies between classes should be explicit

36 Classes and ObjectsInheritance Have the parent class store the values

37 Classes and ObjectsInheritance Have the parent class store the values class InterpolatedSignal(object): def __init__(self, values): self.values = values[:] def get(self, where): raise NotImplementedError('Must provide get!') def find(self, where):...as before...

38 Classes and ObjectsInheritance Have the parent class store the values class InterpolatedSignal(object): def __init__(self, values): self.values = values[:] def get(self, where): raise NotImplementedError('Must provide get!') def find(self, where):...as before...

39 Classes and ObjectsInheritance The child's constructor relies on the parent's class StepSignal(InterpolatedSignal): def __init__(self, values): InterpolatedSignal.__init__(self, values) def get(self, where): i = self.find(where) return self.values[i][1]

40 Classes and ObjectsInheritance The child's constructor relies on the parent's class StepSignal(InterpolatedSignal): def __init__(self, values): InterpolatedSignal.__init__(self, values) def get(self, where): i = self.find(where) return self.values[i][1]

41 Classes and ObjectsInheritance Other classes are just as easy Class LinearSignal(InterpolatedSignal): def __init__(self, values): InterpolatedSignal.__init__(self, values) def get(self, where): i = self.find(where) return y0 + (y1-y0) * (where-x0)/(x1-x0)

42 Classes and ObjectsInheritance class InterpolatedSignal(object): def __init__(self, values): assert len(values) > 0, 'Must have some pairs' for i in range(len(values)): assert len(values[i]) == 2, 'Entries must be pairs' for i in range(len(values)-1)): x0 = values[i][0] x1 = values[i][1] assert x0 < x1, 'Samples must increase on x' >>> signal = StepSignal([ [1., 0.], [0., 2] ]) AssertionError: Samples must increase on x

43 Classes and ObjectsInheritance Child overrides parent method signal InterpolatedSignal object StepSignal __init__ find get

44 Classes and ObjectsInheritance Overriding in action

45 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye'

46 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir'

47 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' Child overrides Parent

48 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' p = Parent() p.hello() hello p.goodbye() goodbye

49 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' p = Parent() p.hello() hello p.goodbye() goodbye C = child() c.hello() hello

50 Classes and ObjectsInheritance Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' class Child(Parent): def goodbye(self): print 'au revoir' p = Parent() p.hello() hello p.goodbye() goodbye C = child() c.hello() hello c.goodbye() au revoir

51 January 2011 Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. created by Greg Wilson


Download ppt "Inheritance Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See"

Similar presentations


Ads by Google