Class Attributes and Method Kinds
Scenario: Every service shares the same category label but keeps its own status.
New words, in plain English
- Class attribute - data shared through the class
- Instance method - a method receiving self
- Class method - a method receiving cls, often an alternate constructor
- Static method - a namespaced function receiving neither automatically
Put shared constants on the class and per-object state on instances. @classmethod can construct from another representation. Use @staticmethod sparingly when a helper belongs conceptually to the class.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
class Service:
kind = "network"
def __init__(self, name): self.name = name
@classmethod
def from_url(cls, url): return cls(url.split("//")[-1])
Tip: A mutable class attribute is shared by every instance, often accidentally.
Goal: Practice this idea in the py-small-class challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.