In [1]:
# Styling
from IPython.core.display import HTML
def css_styling():
sheet = './css/custom.css'
styles = open(sheet, "r").read()
return HTML(styles)
css_styling()
Out[1]:
If you are following this course and do not know how to obtain the above requirements, see Setup Instructions.
In [2]:
# Run this cell before trying examples
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
"Object-oriented programming (OOP) refers to a type of computer programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure."
Source: Webopedia
Note: Encapsulation is sometimes also used in OOP to describe the grouping of data with methods. It is however more common for texts to use it to describe the hiding of data as will be done here.
Useful explanations of these concepts for Python can also be found here
In [3]:
# Numpy arrays are classes
import numpy as np
a = np.array([0, 1, 6, 8, 12])
print(a.__class__)
print(type(a))
In [4]:
# We want to operate on the array: try numpy cumulative sum function
print(np.cumsum(a))
In [5]:
# np.cumsum('helloworld') # Should we expect this to work?
In [6]:
# cumsum is a method belonging to a
a.cumsum()
Out[6]:
In [7]:
class Greeter(object):
def hello(self): # Method (more on 'self' later)
print("Hello World")
agreeter = Greeter() # 'Instantiate' the class
print(agreeter)
# agreeter. # Tab complete?
There's a few things here which I haven't introduced, but all will become clear in the remainder of this workshop.
In [8]:
# Note that we don't pass an argument to hello!
agreeter.hello()
Note: Passing of self
is done implicitly in other languages e.g. C++ and Java, and proponents of those languages may argue that this is better. "Explicit is better than implicit" is simply the python way.
More info: The Constructor creates the instance, and the Initialiser Initialises its contents. Most languages e.g. C++ refer to these interchangably and perform these steps together, however the new style classes in Python splits the process.
The difference is quite fine, and for most purposes we do not need to redefine the behaviour of __new__
. This is discussed in several Stack Overflow threads, e.g.
In [9]:
class A(object):
def __init__(self):
print("Hello")
a_instance = A()
print(type(a_instance))
In [10]:
class Container(object):
"""Simple container which stores an array as an instance attribute
and an instance method"""
def __init__(self, N):
self.data = np.linspace(0, 1, N)
def plot(self):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(self.data, 'bx')
mydata = Container(11) # 11 ia passed as 'N' to __init__
print(mydata.__dict__) # __dict__ is where the attr: value
# pairs are stored!
mydata.plot()
In [11]:
class Container(object):
data = np.linspace(0, 1, 5) # class attribute
def __init__(self):
pass
a, b = Container(), Container()
print(a.data)
print(b.data)
In [12]:
a.data = 0 # Creates INSTANCE attribute
Container.data = 100 # Overwrites CLASS attribute
print(a.data)
print(b.data)
Note: There's a couple of things going on in this example which are worth elaborating on. By specifying ClassName.attribute
, in this case Container.data = 100
we've overwritten the value of data
that EVERY instance of the Container
class will access. Hence printing b.data
gives the expected result.
By setting a.data
at the same time, we have set an instance attribute, which is given priority and called first even though we overwrote the class attribute after assigning this.
This could create a hard to track bug. To avoid it:
instance.attr
unless you really know what you're doing (even then, it's probably better and more readable to make it an instance attribute)For a really in depth explanation of class vs instance attributes, see either of the following links:
In [13]:
class Container(object):
def __init__(self, N):
self.data = np.linspace(0, 1, N)
def print_data(self):
print(self.data)
a = Container(11)
a.print_data() # <<< This is better
Container.print_data(a)
In [14]:
class Fruit(object):
def __init__(self):
self._hasjuice = True
def juice(self):
if not self.isfull(): raise ValueError('No juice!')
self._hasjuice = False
def isfull(self):
return self._hasjuice
orange = Fruit()
print(orange.isfull())
orange.juice()
print(orange.isfull())
In [15]:
# orange._ # tab completion behaviour?
# orange._ # tab completion behaviour now?
orange._hasjuice = True # bad!
orange.isfull()
Out[15]:
In [16]:
class Fruit(object):
def __init__(self):
self.__hasjuice = True
def juice(self):
if not self.isfull(): raise ValueError('No juice!')
self.__hasjuice = False
def isfull(self):
return self.__hasjuice
apple = Fruit()
In [17]:
# apple._ # tab completion behaviour?
apple.juice()
apple._Fruit__hasjuice = False # Definitely bad!
apple.isfull()
Out[17]:
Note: This behaviour can be over used in Python. Programmers from C++ or Java backgrounds may want to make all data hidden or private
and access the data with 'getter' or 'setter' functions, however it's generally accepted by Python programmers that getters and setters are unnecessary. The Pythonista phrase is "we are all consenting adults here", meaning you should trust the programmer to interact with your classes and they should trust you to document/indicate which parts of the data not to touch unless they know what they're doing (hence the underscore convention). See the top answer on this Stack Overflow thread.
For an entertaining view of encapsulation, see this blog
In [18]:
# Live coding Bay....
In [19]:
class Parent(object):
# Note the base __init__ is overridden in
# Child class
def __init__(self):
pass
def double(self):
return self.data*2
class Child(Parent):
def __init__(self, data):
self.data = data
achild = Child(np.array([0, 1, 5, 10]))
achild.double()
Out[19]:
In [20]:
class Plottable(object):
def __init__(self, data):
self.data = data
def plot(self, ax):
ax.plot(self.data)
class SinWave(Plottable):
def __init__(self):
super().__init__(
np.sin(np.linspace(0, np.pi*2, 101)))
class CosWave(Plottable):
def __init__(self):
super().__init__(
np.cos(np.linspace(0, np.pi*2, 101)))
fig = plt.figure()
ax = fig.add_subplot(111)
mysin = SinWave(); mycos = CosWave()
mysin.plot(ax); mycos.plot(ax)
Notes:
super
here as Python 3 allows thissuper
requires additional arguments in Python 2, e.g.
super(Class, self).method(args...)
If you were wondering why we should use super().method
instead of BaseClass.method
, other than the convenience of renaming classes, it relates to multiple inheritance which is beyond the scope of this course. If you need to write programs with multiple inheritance (and there are strong arguments against this), you may want to look at this blog for advanced use of super
.
In [21]:
dir(object)
Out[21]:
In [22]:
class Wave(object):
def __init__(self, freq):
self.freq = freq
self._data = np.sin(np.linspace(0, np.pi, 101)
* np.pi*2 * freq)
def __str__(self):
"""RETURNS the string for printing"""
return "Wave frequency: {}".format(self.freq)
def __lt__(self, wave2):
return self.freq < wave2.freq
wav_low = Wave(10)
wav_high = Wave(50) # A high frequency wave
print(wav_high)
wav_low < wav_high
Out[22]:
In [23]:
# Live coding Bay....
Note: Magic methods are very briefly introduced here. For an extensive overview of magic methods for Python classes, view Rafe Kettlers blog
In [24]:
class OldSyntax:
pass
class NewSyntax(object): # This means 'inherit from object'
pass
print(type(OldSyntax)) # Would give <type 'classobj'>
# in Python 2
print(type(NewSyntax))
object
in Py3Notes: There are other differences affecting classes which we have not included, such as metaclasses and iterator behaviour, but here is a link to a more complete comparison: