Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Implement __str__ for a Point ClassE02. Define __repr__ for a Person ObjectE03. Compare Objects Using __eq__E

Problem No 2

Define __repr__ for a Person Object

Easy

10 minute session

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.

Code editor
Loading editor…

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.

09:20 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.