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]:
/* * @Author: paul * @Date: 2016-02-25 17:35:32 * @Last Modified by: paul * @Last Modified time: 2016-02-25 17:45:54 */

Python Object Orientation Workshop

Paul Chambers

P.R.Chambers@soton.ac.uk

Prerequisites

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

Intro to Python OOP

(For The Classy Programmer)

"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

Why OOP?

  • Naturally structured data
  • Functions used in context
  • Reduce duplicate code
  • Maintainability in large codes/software
  • Many other reasons

OOP in Scientific Computing

  • Java, C++ and Python designed for OOP
  • Everything in Python is an object
  • Scientific libraries, visualisation tools etc.
  • Pseudo Object Orientation in C and Fortran

How can this course help me

  • Language in OOP is very different
    • Learn language used in eg. C++, Java
  • Ability to read code is essential
  • Write/migrate code for community library
    • Better world! Work recognition etc...

Language: Four Fundamental Concepts

  • Inheritance

    • Reuse code by deriving from existing classes
  • Encapsulation

    • Data hiding

Language: Four Fundamental Concepts (2)

  • Abstraction

    • Simplified representation of complexity
  • Polymorphism

    • API performs differently based on data type

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

Material we will cover

  • Classes
  • Initialization and __init__
  • self
  • Encapsulation with underscore '_'
  • Inheritance
  • Magic operators
  • Python 2.X vs 3.X

Classes: Basics

  • Attributes (data)
  • Methods (Functions operating on the attributes)
  • 'First class citizens': Same rights as core types
    • pass to functions, store as variables etc.

Example: Numpy arrays showing how it's done


In [3]:
# Numpy arrays are classes
import numpy as np
a = np.array([0, 1, 6, 8, 12])
print(a.__class__)
print(type(a))


<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

In [4]:
# We want to operate on the array: try numpy cumulative sum function
print(np.cumsum(a))


[ 0  1  7 15 27]

In [5]:
# np.cumsum('helloworld')   # Should we expect this to work?

Example: Numpy arrays showing how it's done (ctd.)

  • We only know what a cumulative sum means for a narrow scope of data types

  • Group them together with an object!


In [6]:
# cumsum is a method belonging to a
a.cumsum()


Out[6]:
array([ 0,  1,  7, 15, 27])

Example: Simple class

  • For now, assume all classes defined by: class ClassName(object)

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?


<__main__.Greeter object at 0x10e7ef5c0>

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()


Hello World

Classes: Initialisation and self

  • __init__ class method
  • Called on creation of an instance
  • Convention: self = instance
  • Implicit passing of self, explicit receive

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.

What is an "instance"?

  • Class is like a type
  • Instance is a specific realisation of that type
  • eg. "Hello World" is an instance of string
  • Instances attributes are not shared

Classes: Initialisation vs Construction

  • Initialisation changes the instance when it is made
  • ... __init__ is not technically Construction (see: C++)
  • __new__ 'constructs' the instance before __init__
  • __init__ then initialises the content

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.

Example: Class Initialisation


In [9]:
class A(object):
    def __init__(self):
        print("Hello")

a_instance = A()
print(type(a_instance))


Hello
<class '__main__.A'>

Instance attributes and Class attributes

  • Instance attributes definition: self.attribute = value
  • Class attributes defined outside functions (class scope)
  • Class attributes are shared by all instances
  • Be careful with class attributes

Example: Defining Instance attributes/methods


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()


{'data': array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ])}

Example: Class attributes vs instance attributes


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)


[ 0.    0.25  0.5   0.75  1.  ]
[ 0.    0.25  0.5   0.75  1.  ]

In [12]:
a.data = 0                     # Creates INSTANCE attribute
Container.data = 100           # Overwrites CLASS attribute
print(a.data)
print(b.data)


0
100

Class vs Instance attributes: priority

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:

  • Stick to instance variables unless you specifically need to share data e.g. constants, total number or list of things that are shared
  • Don't overwrite things you know are class attributes with 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:

Example: Implicit vs Explicit passing Instance


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)


[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1. ]
[ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1. ]

Exercise: Basics and Initialisation

Python OOP 1 Basics and Initialisation with images

Classes: Encapsulation

  • Hiding data from users (and developers)
  • Use underscores '_' or '__'
  • Useful if data changing should be controlled
  • Convention only in Python - not enforced!

Single vs Double Underscore

  • Single underscore

    • Nobody outside this class or derived classes should access or change
  • Double underscore

    • Stronger attempt to enforce the above
    • Also 'mangles' the attribute name with

instance._ClassName__Attribute

Example: Data hiding "protected", single underscore


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())


True
False

In [15]:
# orange._                  # tab completion behaviour?
# orange._                 # tab completion behaviour now?
orange._hasjuice = True    # bad!
orange.isfull()


Out[15]:
True

Example: Data hiding "private", double underscore


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]:
False

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....

Classes: Inheritance

  • Group multiple objects and methods
  • Child/Derived class inherits from Parent/Base
  • Reduce duplicate code
  • Maintanable: changes to base falls through to all
  • Beware multiple inheritance rules - we won't cover this

Example: Simple inheritance


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]:
array([ 0,  2, 10, 20])

Example: Calling parent methods with super


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:

  • We didn't need any arguments to super here as Python 3 allows this
  • super 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.

Classes: Magic Methods

  • The workhorse of how things 'just work' in Python
  • object & builtin types come with many of these
  • Surrounded by double underscores
  • We've seen one already: __init__!

Example: The magic methods of object


In [21]:
dir(object)


Out[21]:
['__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__']

Magic Methods: Closer look

  • __lt__:

    • Called when evaluating
        a < b
  • __str__

    • Called by the print function
  • All magics can be overridden

Example: overriding __str__ and __lt__ magic methods


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


Wave frequency: 50
Out[22]:
True

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

Exercise: Inheritance and Magic Methods

Python OOP2 Inheritance and Magic Methods using shapes

Python 2 vs 3

  • Dont need to inherit object in Python 3
  • New classes inherit from object
  • Inheritance behaviour is different
  • Old classes removed in Python 3

Example: New class default Python 3


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))


<class 'type'>
<class 'type'>
  • Backwards compatibility: inherit object in Py3

Notes: 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:

Extra Material

  • Harder exercise (if time)

  • Should test your knowledge from this course

Exercise: Predator Prey

Summary

  • Covered the language and concepts for Object Orientation (Transferable!)
  • OOP Implementation in Python
  • Read the packages you use daily
  • Create maintainable packages for yourself and scientific community

Thank You

P.R.Chambers@soton.ac.uk