PNL Learn

Object-Oriented Python

Design your own types with classes, inheritance and dataclasses.

Stop passing loose dicts around and design your own types. You will write classes with state and behaviour that guard their own rules, readable reprs, class attributes and alternative constructors, then use inheritance, polymorphism and super() correctly. You will read the method resolution order of multiple inheritance and mixins, make super() cooperate in a diamond, and inspect objects and classes with hasattr, getattr and __dict__. You will make objects compare, sort, hash and act like containers and iterators with dunder methods, protect invariants with properties, name mangling and abstract base classes, and finish with dataclasses and four small multi-class designs: a library, a warehouse, a shapes hierarchy and a parking lot.

IntermediatePython8 lessons90 exercisesAbout 6.5 h
Create a free account

What you will learn

  • Classes and instances
  • Methods and state
  • Inheritance and super()
  • Multiple inheritance and the MRO
  • Introspection
  • Dunder methods and iterators
  • Properties and abstract base classes
  • Dataclasses and composition

Lessons

  1. 1

    Classes and Instances

    Define your own types with class and __init__, understand self, and give every object its own state.

    13 exercises

  2. 2

    Methods and State

    Write methods that change an object safely, build methods from other methods, keep collections inside objects, and make objects print readably with __repr__ and __str__.

    11 exercises

  3. 3

    Class Attributes, classmethod and staticmethod

    Share data across every instance without sharing bugs, see where attributes live with __dict__, build objects from other formats with @classmethod, and know when @staticmethod fits.

    11 exercises

  4. 4

    Inheritance

    Build subclasses that override and extend their parents with super(), let one call work on many types, check types with isinstance, and recognize when inheritance is the wrong tool.

    11 exercises

  5. 5

    Multiple Inheritance and Introspection

    Combine parents and mixins, predict which method wins from the MRO, make super() cooperate in a diamond, and inspect objects and classes with hasattr, getattr, __name__, __module__ and __bases__.

    11 exercises

  6. 6

    Dunder Methods

    Make your objects compare, sort, add, hash and behave like containers and iterators by implementing Python's special methods.

    11 exercises

  7. 7

    Encapsulation and Properties

    Signal what is internal, see what name mangling really does, validate attributes with @property, design to behaviour with duck typing, and enforce interfaces with abstract base classes.

    12 exercises

  8. 8

    Dataclasses and Composition: Putting It Together

    Cut boilerplate with @dataclass, keep value objects frozen, prefer composition over inheritance, know when not to write a class, then design four small multi-class systems.

    10 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 31
  • Fix the bug: 19
  • Type the answer: 11
  • Predict the output: 10
  • Multiple choice: 7
  • Fill in the blank: 5
  • Match the pairs: 3
  • Select all that apply: 2
  • Spot the bug: 1
  • Reorder lines: 1

Aligned to

PCAP-31-03Python InstituteSource
  • Object-Oriented Programming
    • 4.1Understand the object-oriented approach
    • 4.2Employ class and object properties
    • 4.3Equip a class with methods
    • 4.4Discover the class structure
    • 4.5Build a class hierarchy using inheritance
    • 4.6Construct and initialize objects
CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-FundamentalsFundamental Programming Concepts and Practices

Proto Node Labs is not affiliated with or endorsed by Python Institute and ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.