Model one ice-cream scoop as a Python object with a single flavor attribute. Define Scoop as a class, then create instances by passing each flavor to Scoop(). section 9.1 section 9.2
Implement Scoop by following these steps:
- Define the class with the class keyword. section 9.2
- Use __init__(self, flavor) to assign self.flavor = flavor on each new instance. section 9.2
- Create multiple Scoop objects and store them in a list, preserving each object's flavor attribute. section 9.1 section 9.7
- Use self as the conventional first method parameter; Python does not reserve that name, but developers and tools expect it. section 9.2
Create and collect scoops
class Scoop: def __init__(self, flavor): self.flavor = flavor scoops = [Scoop(flavor) for flavor in ('chocolate', 'vanilla', 'persimmon')] for scoop in scoops: print(scoop.flavor) section 9.1 section 9.2
Visualize three Scoop instances and their relationship to the Scoop class. section 9.1
