O'Reilly logo
live online training icon Live Online training

Intermediate Python Classes

Making Pythonic Classes

Topic: Software Development
Trey Hunner

Python’s classes are flexible and powerful. While learning the syntax for creating classes isn’t terribly difficult, understanding how to structure your classes well takes some practice.

Expert Trey Hunner walks you through a number of advanced features that will make your classes blend in more with other Python objects and feel more natural and Pythonic. You’ll learn how to add support for various Python operators and protocols to your classes, primarily by learning how Python’s various dunder methods work. Join in to discover how to write classes that other Python programmers will quickly and easily understand at a glance.

What you'll learn-and how you can apply it

By the end of this live online course, you’ll understand:

  • Why you don’t use using getter and setter methods in Python
  • How to embrace “duck typing” with Python classes
  • How to use dataclasses to make small but powerful classes

And you’ll be able to:

  • Control how attribute looks (“my_obj.my_attribute”) work on your Python objects
  • Overload many of Python’s operators on your own classes
  • Make your own custom data structures that wrap around built-in data structures

This training course is for you because...

  • You’re a Python programmer.
  • You work with classes in Python often.
  • You want to understand how to make classes that feel more “native” in Python.

Prerequisites

  • A working knowledge of Python’s data structures
  • Experience creating classes in Python
  • A computer with Python 3.6+ and the code editor of your choice installed (instructions)

_Complete the following setup instructions: _

  • Create a directory to use for this workshop in your home directory or desktop (or wherever you want to save your work). This folder will be referred to as "python_class" in this course, but you can name it whatever you'd like. Just don't forget where you put it.
  • Download the ZIP file of course exercises at https://trey.io/intermediate-classes-olt-exercises and extract it into the python_class directory you just made.
  • When unzipped, that ZIP file will create a new folder called "exercises." It contains the test framework you’ll be using as well as the files you’ll need to modify for most of the exercises in the workshop.
  • Open up your system command prompt or terminal (not a Python prompt) and change directories to that exercises directory. On Windows you may need to type "C:" or another drive letter to change drives first. There are a couple shortcuts in Windows for opening a command prompt in the correct directory quickly. (See https://trey.io/cmd1 and https://trey.io/cmd2 for examples.)
  • Once in the exercises directory, open a command prompt or terminal and run "py -3 test.py is_ok" (Windows) or "python3 test.py is_ok" (Linux/macOS). You should see a success message that includes "Congrats and welcome to the Test Framework!" and ends with "OK."

Recommended preparation:

Recommended follow-up:

About your instructor

  • Trey Hunner helps software teams level up their Python skills through on-site Python training workshops and courses. Trey speaks at Python conferences, blogs about Python frequently, and runs Python Morsels, an online Python education service for individuals and small teams.

    Trey was formerly a director at the Python Software Foundation.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Class behavior you may not know (45 minutes)

  • Presentation: How method calls and attribute lookups work
  • Hands-on exercise: Create simple classes
  • Q&A

Properties (40 minutes)

  • Presentation: Why we don’t use getters/setters; how data hiding works
  • Hands-on exercise: Create properties as an alternative to getters/setters and hiding data
  • Q&A

Break (5 minutes)

Operator overloading (40 minutes)

  • Presentation: Dunder methods; operator overloading basics
  • Hands-on exercise: Practice overloading arithmetic and comparison operators
  • Q&A

Break (5 minutes)

Inheritance best practices (40 minutes)

  • Presentation: Delegating to children/parents; duck typing; abstract base classes
  • Hands-on exercise: Create custom collections via abstract base classes
  • Q&A

Break (5 minutes)

Class creation helpers (25 minutes)

  • Presentation: Named tuples and data classes
  • Hands-on exercise: Work with dataclasses
  • Q&A

Break (5 minutes)

  • Advanced dunders (30 minutes)
  • Presentation: Memory efficiency, iterability, and more
  • Hands-on exercise: Go over “take home” exercises to do on your own later
  • Q&A