Python Quest

Track 11 · Lesson 5 of 12

Class attributes, classmethods and staticmethods

Share data across all objects with class attributes, avoid the shared-mutable trap, and add alternate constructors with classmethod.

About 40 minIntermediate4 exercises

You will be able to

  • Tell class attributes (shared by every object) from instance attributes (one per object)
  • Use a class attribute for constants and for counting objects
  • Recognise and fix the shared mutable class attribute bug
  • Write a classmethod that builds objects from another format (an alternate constructor)
  • Use a staticmethod for a helper that belongs with the class but needs no object

Included with a Formula Quest membership

It has 4 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.

View Formula Quest memberships

Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.