Define __repr__ when an object needs a developer-oriented printed representation. Conventionally, it returns unambiguous text that may be valid Python syntax; print(obj) uses it when __str__ is absent. section 10.10
Work through the representation strategy:
- Represent contained values by collecting their printed representations, then combine them with str.join. section 10.10
- Use a generator expression to format each contained object lazily; a cage can place each animal on a separate indented line. section 10.30
- Add __str__ only when developer and end-user output should differ; otherwise, __repr__ can serve both uses. section 10.10
Cage report
Create Cage(1), add wolf and sheep, and make print(c1) show the cage ID followed by each animal's representation. Extend the exercise by making subclass representations include an animal sound. section 10.29 section 10.28
Figure 10.8 A Cage instance containing one wolf and one sheep
Inspect a cage instance containing one wolf and one sheep. section 10.30


