liveBook · Manning

Bowl

As explained in Python Workout: 50 ten-minute exercises

Bowl is a book-specific Python class representing a bowl that limits the number of scoops it can contain.

Bowl is a Python class that models a container for Scoop objects. It stores scoops in an ordered self.scoops list, accepts multiple scoops through add_scoops, and displays their flavors through __repr__. section 9.6 section 9.7

A practical Bowl implementation separates shared policy from per-object state:

  • Initialize self.scoops as an empty list so each Bowl owns its contents and preserves scoop order. section 9.7
  • Define add_scoops(self, *new_scoops) to accept any number of Scoop instances and append them one at a time. section 9.7
  • Put max_scoops = 3 on the class to express one shared limit instead of hardcoding the value throughout the implementation. section 9.12
  • Check the current scoop count before adding; once three scoops exist, additional scoops are ignored. section 9.11
  • Refer to self.max_scoops when subclasses may override the capacity, allowing one add_scoops implementation to support Bowl and BigBowl. section 9.17

Adding scoops

s1 = Scoop('chocolate') s2 = Scoop('vanilla') s3 = Scoop('persimmon') b = Bowl() b.add_scoops(s1, s2) b.add_scoops(s3) print(b) The bowl contains three Scoop objects, while its representation joins their flavor strings for display. A fourth scoop would be ignored after the capacity limit is reached. section 9.6 section 9.7 section 9.11

s1 = Scoop('chocolate')
s2 = Scoop('vanilla')
s3 = Scoop('persimmon')

b = Bowl()
b.add_scoops(s1, s2)
b.add_scoops(s3)
print(b)

Inspect the complete interaction sequence for constructing Scoop objects, adding them across multiple calls, and printing the Bowl. section 9.6

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains state, method arguments, representation, attribute lookup, and inheritance, stopping short of broader object-model internals.
Examples
Uses Scoop objects, ice-cream flavors, repeated add_scoops calls, a three-scoop limit, and a BigBowl subclass.
What it assumes
Assumes familiarity with Python classes and Scoop objects while developing lists, methods, class attributes, and inheritance through the example.
What you can do after
Build it from scratch: create a compositional Python class with shared limits, readable output, and subclass-specific capacity.
How it connects
Connects classes, instances, class attributes, lists, inheritance, variadic arguments, and object representations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.