Classes and Objects Inheritance Copyright Software Carpentry 2010
Classes and Objects 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.
strength Interpolating time series signals time Classes and Objects Inheritance
strength Interpolating time series signals time linear interpolation step function Classes and Objects Inheritance
strength Interpolating time series signals time linear interpolation A lot of the implementation is the same step function Classes and Objects Inheritance
strength Interpolating time series signals time linear interpolation A lot of the implementation is the same How can we eliminate step function Classes and Objects the reduncancy? Inheritance
First implementation class Step. Signal(object): . . . def get(self, where): if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return y 0 raise Index. Error, '%f too high' % where Classes and Objects Inheritance
Second implementation class Linear. Signal(object): . . . def get(self, where): if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return y 0 + (y 1 -y 0) * (where-x 0) / (x 1 -x 0) raise Index. Error, '%f too high' % where Classes and Objects Inheritance
Second implementation class Linear. Signal(object): . . . def get(self, where): if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return y 0 + (y 1 -y 0) * (where-x 0) / (x 1 -x 0) raise Index. Error, '%f too high' % where Classes and Objects Inheritance
Refactor class Step. Signal(object): def get(self, where): i = self. find(self, where) return self. values[i][1] class Linear. Signal(object): def get(self, where): i = self. find(self, where) x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] return y 0 + (y 1 -y 0) * (where-x 0)/(x 1 -x 0) Classes and Objects Inheritance
Refactor class Step. Signal(object): def get(self, where): i = self. find(self, where) return self. values[i][1] Where to put find? class Linear. Signal(object): def get(self, where): i = self. find(self, where) x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] return y 0 + (y 1 -y 0) * (where-x 0)/(x 1 -x 0) Classes and Objects Inheritance
Refactor class Step. Signal(object): def get(self, where): i = self. find(self, where) return self. values[i][1] Where to put find? Don't want to duplicate class Linear. Signal(object): def get(self, where): i = self. find(self, where) x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] return y 0 + (y 1 -y 0) * (where-x 0)/(x 1 -x 0) Classes and Objects Inheritance
Use inheritance Classes and Objects Inheritance
Use inheritance class Parent(object): def hello(self): print 'hello' Classes and Objects Inheritance
Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Classes and Objects Inheritance
Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent Classes and Objects Inheritance
Use inheritance class Parent(object): def hello(self): print 'hello' c = Child() c. goodbye() goodbye class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent Classes and Objects Inheritance
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 Classes and Objects Inheritance
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 Classes and Objects Inheritance
Use inheritance class Parent(object): def hello(self): print 'hello' class Child(Parent): def goodbye(self): print 'goodbye' Child inherits from Parent Classes and Objects c = Child() c. goodbye() goodbye c. hello() hello p = Parent() p. hello() hello p. goodbye() Attribute. Error: 'Parent' object has no attribute 'goodbye' Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
Contents of memory object Parent hello Child goodbye c p Classes and Objects Inheritance
class Interpolated. Signal(object): def find(self, where): if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return i raise Index. Error, '%f too high' % where Classes and Objects Inheritance
class Interpolated. Signal(object): def find(self, where): if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return i raise Index. Error, '%f too high' % where Not much use on its own Classes and Objects Inheritance
class Interpolated. Signal(object): Where does this def find(self, where): come from? if where < self. values[0][0]: raise Index. Error, '%f too low' % where for i in range(len(self. values)-1): x 0, y 0 = self. values[i] x 1, y 1 = self. values[i+1] if x 0 <= where <= x 1: return i raise Index. Error, '%f too high' % where Not much use on its own Classes and Objects Inheritance
Derive specific interpolators Classes and Objects Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): def __init__(self, values): self. values = values[: ] def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): def __init__(self, values): self. values = values[: ] def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): def __init__(self, values): self. values = values[: ] def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): def __init__(self, values): self. values = values[: ] def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): def __init__(self, values): self. values = values[: ] def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Fragile Inheritance
Derive specific interpolators class Step. Signal(Interpolated. Signal): 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 Classes and Objects Inheritance
Have the parent class store the values Classes and Objects Inheritance
Have the parent class store the values class Interpolated. Signal(object): def __init__(self, values): self. values = values[: ] def get(self, where): raise Not. Implemented. Error('Must provide get!') def find(self, where): . . . as before. . . Classes and Objects Inheritance
Have the parent class store the values class Interpolated. Signal(object): def __init__(self, values): self. values = values[: ] def get(self, where): raise Not. Implemented. Error('Must provide get!') def find(self, where): . . . as before. . . Classes and Objects Inheritance
The child's constructor relies on the parent's class Step. Signal(Interpolated. Signal): def __init__(self, values): Interpolated. Signal. __init__(self, values) def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
The child's constructor relies on the parent's class Step. Signal(Interpolated. Signal): def __init__(self, values): Interpolated. Signal. __init__(self, values) def get(self, where): i = self. find(where) return self. values[i][1] Classes and Objects Inheritance
Other classes are just as easy Class Linear. Signal(Interpolated. Signal): def __init__(self, values): Interpolated. Signal. __init__(self, values) def get(self, where): i = self. find(where) return y 0 + (y 1 -y 0) * (where-x 0)/(x 1 -x 0) Classes and Objects Inheritance
class Interpolated. Signal(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)): x 0 = values[i][0] x 1 = values[i][1] assert x 0 < x 1, 'Samples must increase on x' >>> signal = Step. Signal([ [1. , 0. ], [0. , 2] ]) Assertion. Error: Samples must increase on x Classes and Objects Inheritance
Child overrides parent method object Interpolated. Signal __init__ find get Step. Signal __init__ get signal Classes and Objects Inheritance
Overriding in action Classes and Objects Inheritance
Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' Classes and Objects Inheritance
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' Classes and Objects Inheritance
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 Classes and Objects Inheritance
Overriding in action class Parent(object): def hello(self): print 'hello' def goodbye(self): print 'goodbye' p = Parent() p. hello() hello p. goodbye() goodbye class Child(Parent): def goodbye(self): print 'au revoir' Classes and Objects Inheritance
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' Classes and Objects p = Parent() p. hello() hello p. goodbye() goodbye C = child() c. hello() hello Inheritance
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' Classes and Objects p = Parent() p. hello() hello p. goodbye() goodbye C = child() c. hello() hello c. goodbye() au revoir Inheritance
created by Greg Wilson 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.
- Slides: 51