Lesson 10 of 12
Structured learning draftKeep instance data separate in a small class
Two stock items must not share the same mutable count accidentally. Build a small class whose constructor stores each item's data on that instance, then compare two objects to see which values belong to which item.
Learning objectives
- Create two independent objects.
- Store instance attributes in __init__.
- Call a method using an object's data.
Keep instance data separate in a small class: the working sequence
Trace the three stages of keep instance data separate in a small class and compare them with the example.
StockItem defines how one item's data and behaviour are organised.
pens and books receive different quantities in separate instances.
The same method evaluates the stored quantity for the object it is called on.
The constructor receives the values for one instance. self refers to that instance inside its methods. A class is useful when data and related behaviour belong together; a simple function is often enough for a standalone calculation.
class StockItem:
def __init__(self, name, quantity):
self.name = name
self.quantity = quantity
def needs_reorder(self, threshold):
return self.quantity <= threshold
pens = StockItem('Pens', 12)
books = StockItem('Books', 3)
print(pens.needs_reorder(5))
print(books.needs_reorder(5))False
TrueTry it, then change it
- Run both objects and explain the two results.
- Change books.quantity to 8 and confirm pens.quantity is still 12.
- Compare this class with the earlier function and explain when each design is useful.
Common mistakes
- Confusing the class definition with an individual instance.
- Keeping per-item mutable data in a shared class attribute.
- Introducing a class for every small calculation without a reason.
Practice activity
Apply the lesson
Add a receive(amount) method that rejects negative amounts. Test that receiving stock for one item does not change another item. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
Which change should update only the books instance?
Lesson summary
- An instance stores data for one object.
- Methods use the instance supplied as self.
- Choose a class when grouping data and behaviour helps.
Sources and further reading
- Classes and instance variablesPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.