Learning Objectives

  • Understand core concepts of object-oriented programming
  • Understand the benefits of object-oriented programming
  • Learn how to create your own object classes (see the accompanying notebook)

Object-Oriented Programming? What is this tech-geekery and why should I care?

  • Python is an object-oriented language. Every entity is treated as an object; even single integers are objects of the “int” class.
  • An understanding of object-oriented programming will help give you a better understanding of how the packages you use function.
  • You can use this understanding to create your own programs that harness the strengths of objected-oriented programming:
    • Convenience
    • Flexibility
    • Extensibility
    • Simpler interfacing

History

  • In the early days of programming, variables could only be the “primitive” data types containing a single value
    • Integer, Float, Boolean, Char
  • Later came Structures (“Structs”), which can contain multiple values of different types.
  • Structs were the precursor to objects, but they couldn’t yet contain associated functions within them.
  • Objects first appear in the Simula programming language in the 1960s for modelling physical phenomena.
  • Those objects influenced Alan Kay, who coined the term “object-oriented programming” to describe architecture where objects pass information to one another.
My. Kay looking very pleased with his coinage.

Classes and Objects

  • Classes act as templates for objects
  • Objects are referred to as “instances” of classes
    • We talk of objects being “instantiated” from a class
    • Think of each object as being a copy created using the class template
  • Objects represent entities with their own data (attributes) and behaviours (methods)
  • We can create lots of instances of an object with their own attribute values and call methods on them separately yet consistently
  • Objects are self-contained units that can interact with objects both of the same and of other classes
Consistency is the key.

The Anatomy of a Class

A diagram representing a class at the top and object instances created from the class

Inheritance

  • Child classes inherit attributes and methods from parent classes
  • Child classes can modify / override and add to what they have inherited
  • Reduces code duplication; increases re-usability
  • Improves extensibility: i.e. new classes with the same core behaviours, but new features, can be based on existing classes
Inheritance is a much less contentious issue in Python.

A parent class (green) and two different child classes (blue and orange), each with objects created from them

More Object-Oriented Programming Concepts

Encapsulation for convenience

  • Bundling data (attributes) with functions (methods)
  • Methods are tailor-made to work with the data contained in the object
  • Saves on having to pass data between multiple functions, which is particularly useful in machine learning models
  • Pandas DataFrames demonstrate encapsulation. They contain data, but also have methods associated with them
    • df = pd.DataFrame(data) <– Instantiating a dataframe object
    • df.head(), df.describe(), df.drop() <– calling methods

Polymorphism for flexibility

  • Objects of different types can be treated in the same way, even if the behaviour differs
    • With Pandas DataFrames, .head() will work on both a DataFrame and a Series1
  • “Duck typing”: If the behaviour of a thing matches that of another thing, they are considered the same. In OOP terms, the presence of certain methods is more important than which class an object comes from2
    • The scikit-learn library’s allows the same code to work for different models

Abstraction for simpler interfacing

  • Separating the implementation code from the functionality that users (i.e. other programmers) interact with

  • Creates a simple interface for parts of a program pass information between each other

  • Examples:

    • When working with machine learning models, users only need to apply simple methods to train the model and make a prediction.
    • Entities interacting with each other within a simulation model.

When to use OOP

  • When you want to easily re-use code, to avoid repetition and to extend functionality
  • Discrete Event Simulations for modelling queueing / capacity problems
  • Creating custom, branded visualisation packages, for example an NHS-branded SPC chart
    • Creating a package that can be used to import the latest data from a website without users having to understand API calls or the website’s structure
  • When you want to model real-world entities
    • Discrete Event Simulations for modelling queueing / capacity problems
  • When you want to make code modular and easy for others to work with
  • When you want to simplify end-users’ interaction with Python, fostering a self-service approach to analytics
    • Creating custom, branded visualisation packages, for example an NHS-branded SPC chart
  • Less appropriate for: When you want to be certain of the state of your data at each step of a process, for example when cleansing data

Resources

RealPython: Object-Oriented Programming (OOP) in Python

OOP produces code that is easy to read, extend and maintain

HSMA’s Guide to Object-Oriented Programming

HSMA’s Discrete Event Simulation Module

Thank You!


Contact:

Notebook & Slides:


… And don’t forget to give us your feedback.