Python Classes

__init__, self, instance vs class variables, staticmethod vs classmethod

35 min · interactive lesson with runnable code and 5 graded exercises.

What you'll be able to do

  • Explain what __init__ and self do
  • Tell instance variables from class variables — and avoid shared mutable state
  • Choose between @staticmethod and @classmethod
  • Write alternative constructors that work with subclasses

The full lesson is free with an account.

Classes, __init__ and self

Instance vs class variables

staticmethod vs classmethod

Alternative constructors

Common pitfalls

Classes in ML code