Skip to Content
View all events

Intermediate Python Classes

Published by O'Reilly Media, Inc.

Intermediate content levelIntermediate

A Practical Guide to Effective Pythonic Class Structure

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 live event 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:

Schedule

The time frames are only estimates and may vary according to how the class is progressing.

Class behavior you may not know

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

Properties

  • 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

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

Break (5 minutes)

Inheritance best practices

  • 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

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

Break (5 minutes)

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

Your Instructor

  • Trey Hunner

    Trey Hunner helps Python programmers level up their skills through corporate training and through Python Morsels, a Python skill-building service for individuals and small teams. Trey also sends out a new Python tip every week through his email newsletter at pym.dev/newsletter.

    linkedinXlinksearch

Skill covered

Python