Objects

Overview

This week we will see how Python actually works by looking beyond simple functions and into methods, classes, and the outlines of Object Oriented Design and Programming (OOD/OOP). We’ll also look at what to do when ‘things go wrong’, because they will, but sometimes we want that to blow up the application while other times we want Python to handle the ‘exception’ gracefully. Learning to read Exceptions is essential to debugging code: the one thing that almost never works when you get an exception is ignoring it.

Learning Objectives
  1. Develop enough of an understanding of classes and inheritance that you can make effective use of Python.
  2. Develop an understanding of how to read and write Exceptions so as to be able to create robust code.
  3. Under the process of moving code from in-line scripting, to functions, to packages of functions.
  4. Begin developing an appreciation of the substantive concerns of the module (data, ethics, bias, and the risks of ‘experts’ who ‘know it all’).

This week deals with ‘objects’ and ‘classes’, which is fundamental to mastering the Python programming language: in Python, everything is an object, you just didn’t need to know it until now. Understanding how classes and objects work is essential to using Python effectively, but it will also make you a better programmer in any language because it will help you to think about how data and code work together to achieve your goals.

Lectures

Come to class prepared to present:

Session Video Presentation
Methods Video Slides
Classes Video Slides
Design Video Slides
Exceptions Video Slides

Other Prep

  • Come to class prepared to present/discuss:
    • Cox and Slee (2016) <PDF>
    • D’Ignazio and Klein (2020), chap. 5, Unicorns Janitors, Ninjas, Wizards, and Rock Stars <URL>
  • We’ll aim to do the ‘Student Dialogue’ Mentimeter poll at the start of class.
Connections

We’re now going to shift our focus slightly towards more critical takes on data and data science. These are not by people who can’t ‘do’ data science, rather they point to ways in which neither the data, nor the people who process them, are neutral or objective in the way that we often like to think they are. Cox and Slee (2016) is also a first introduction to the underlying data that we’ll be working with over the rest of term. D’Ignazio and Klein (2020) should be getting you thinking about how ‘cleaning’ is not just about hygiene but has real implications for what you can and will find in your data: data is often a lot less ‘tidy’ than tidyr might lead you think!

Practical

:::{.callout-warning}

This practical will lead you through the process of converting inline scripts into functions and, ultimately, into a simple package. The last parts of the practical are optional – creating classes in hierarchies is integral to how Python works, but many data scientists will rarely need to write their own classes… they just make use of classes written by others (which is why understanding what they are is important, but being able to write your own is a little less so).

Connections

The practical focusses on:

  • Bedding in the ‘data thinking’ from last week’s practical.
  • Creating functions to perform repetitive tasks.
  • Packaging these functions up and accessing via the appropriate namespace.
  • Implementing a few simple classes so that you understand the basics of how they work.

To access the practical:

  1. Preview on GitHub
  2. Download the Notebook

References

Cox, M., and T. Slee. 2016. “How Airbnb’s Data Hid the Facts in New York City.” Inside Airbnb. http://insideairbnb.com/reports/how-airbnbs-data-hid-the-facts-in-new-york-city.pdf.
D’Ignazio, Catherine, and Lauren F. Klein. 2020. Data Feminism. MIT Press. https://bookbook.pubpub.org/data-feminism.