Classes and Instances
Scenario: A monitoring program passes several related service values through every function.
New words, in plain English
- Class - a blueprint defining data and behavior
- Instance - one object created from a class
- Attribute - data stored on an object
- Method - a function defined on a class
Classes group state and behavior when those belong together. Creating an instance calls the class. self is the current instance and is passed automatically to instance methods.
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:
def healthy(self):
return self.status == "up"
api = Service()
api.status = "up"
print(api.healthy())
Tip: Use a class when behavior protects or explains related state, not merely to wrap one dict.
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.