easy +10 pts

String Representation Mixin

Create a mixin that auto-generates __repr__ and __str__ for classes.

Implement a mixin class `ReprMixin` that provides generic `__repr__` and `__str__` methods for any class that inherits from it. The methods should be defined as instance methods. - `__repr__` should return a string of the form: `ClassName(attr1=value1, attr2=value2, ...)` where the attributes are the instance's `__dict__` items, sorted by attribute name, and the values are their string representations. - `__str__` should return the same string as `__repr__`. - For example, if an instance has attributes `{'name': 'Alice', 'age': 30}`, then `__repr__()` should return `'Person(age=30, name="Alice")'` (note: quotes around string values come from `repr(value)`). - The mixin should work for any subclass, using the actual class name of the instance. - You must not override `__repr__` or `__str__` in the subclass; the mixin provides them. Write the class `ReprMixin` with exactly the above behavior. The `__init__` method is not needed; subclasses define their own initialization. Ensure the methods do not fail if the instance has no attributes (returns `ClassName()`).

Constraints

- No import restrictions; standard library only. - The class name of the instance is obtained via `type(self).__name__`. - Attribute values are formatted using `repr()`. - Complexity: O(k log k) for sorting attributes, where k is number of attributes.

Example

>>> class Person(ReprMixin):
...     def __init__(self, name, age):
...         self.name = name
...         self.age = age
...
>>> p = Person('Alice', 30)
>>> repr(p)
"Person(age=30, name='Alice')"
>>> str(p)
"Person(age=30, name='Alice')"
>>> class Empty(ReprMixin):
...     pass
>>> repr(Empty())
'Empty()'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `self.__dict__` to get instance attributes as a dictionary.
Sort the attributes by key using `sorted()`.
Format each attribute as `key=repr(value)` and join with ', '.
Use `type(self).__name__` to get the class name.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.