Creating a class
A class is a blueprint for objects. It describes the shape of related data and behavior before any particular object exists.
This is useful when several things in your program share the same kind of information. Instead of treating each one as a special case, you define the common structure once and create as many objects as you need.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Constructors
__init__ sets up the data each new object needs. It runs as the object is being created, which makes it the right place to give the object its first values.
If you imagine a class as a blueprint, then the constructor is the part that fills in the starting details for each new instance.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Methods
Methods are functions attached to an object. They let the object act on its own data instead of forcing the rest of the program to reach in and manage every detail manually.
This is one of the reasons object-oriented code can stay tidy: the behavior sits next to the data it belongs to.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Changing state
Methods can update attributes on self. This means the object can remember what happened last time and behave differently the next time you call one of its methods.
Each Counter object has its own value. Calling increment changes the value stored on the particular object named counter; it does not change every possible counter.
When you work with state, always ask whether you are changing one object, returning a new value, or both. That distinction matters a lot once objects start doing real work.
pet.nameis Mochipet.walksis 0- Calling a method can update only this object’s stored values.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Inheritance
A child class can reuse behavior from a parent class. This lets related objects share common behavior while still changing the parts that are different.
Inheritance is most helpful when the child really is a more specific version of the parent. If the relationship feels forced, it is usually better to keep the classes separate.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.Useful representations
__repr__ gives objects a helpful developer-facing description. When you print an object or inspect it in a debugger, this method helps you see the important parts without digging into the object manually.
A good representation is short, specific, and easy to read while you are debugging.
Pause after running the example and explain each line in your own words. Notice what the program reads, what it stores, and what it prints, then make one small change and run it again to see exactly what changed.
Practice
Work through the same idea a few times so it starts to feel familiar. Begin by copying the example, then make the single change the instruction asks for, and finally explain to yourself why the result changed.
Your program should be an exact copy of the runnable example above, including any indentation.Keep the program structure the same. A correct attempt has one changed value and one short comment about its effect.# I expect: ...
# Then run the program and compare that comment with the real output.A correct solution uses the central idea from this lesson, prints one result, and stays small enough to trace line by line.