Lesson guide
What this Python exercise practices
Define __repr__ for a Person Object is a beginner practice lesson that focuses on python practice, coding exercises, test feedback. It is designed to be solved in about 10 minutes with examples, starter code, and test feedback.
Prerequisites
- Python basics
- Variables
- Reading simple prompts
Difficulty and time
- Level
- Beginner
- Estimated time
- 10 minutes
Practice path
Related public exercises
Summary
Implement a __repr__ method for a Person class that returns an unambiguous constructor-like string.
Problem statement
You are given a Person class with name and age attributes. Implement the __repr__ method to return a string in the following exact format: Person(name=<repr-of-name>, age=<age>) Notes: - Use Python's repr() on the name to ensure proper escaping and quoting. - Age should appear as an integer (no extra formatting). - The result should be a single string; this is intended as a developer-facing representation (like how you'd reconstruct the object). This reinforces writing __repr__ that is unambiguous and helpful in debugging.
Task
Implement __repr__ so that repr(Person(name, age)) returns an unambiguous string like "Person(name='Alice', age=30)" — suitable for debugging and recreation.
Examples
Simple person
Input
repr(Person('Alice', 30))
Output
Person(name='Alice', age=30)
Explanation
repr should include the quoted name and the numeric age.
Input format
A call to repr(Person(name, age)).
Output format
A single string of the form "Person(name=<repr(name)>, age=<age>)" compared as str(return_value).
Constraints
- Use the provided Person class skeleton. - Implement only the __repr__ method. - Do not change attribute names (name, age).
Samples
Sample input 0
repr(Person('', 0))
Sample output 0
Person(name='', age=0)
Explanation 0
Empty name should appear as an empty quoted string; age as 0.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.