Web
Opt
tools
__str__ & __repr__ Magic Methods
Class Name
Class Attributes
Magic Methods
__str__ (String representation)
__repr__ (Developer representation)
Example Type
Basic Example
Advanced Example
Before/After Comparison
Run Visualization
Console Output
>
__str__ & __repr__ Visualization
Current Configuration:
Class:
Car
Example:
basic
Attributes:
brand, model, year
Methods:
Generated Output:
Click "Run Visualization" in the options panel to see output.
__str__ vs __repr__ Comparison:
__str__
✓ User-friendly
✓ Readable
✓ print() output
✓ End-user focused
__repr__
✓ Developer-focused
✓ Unambiguous
✓ Debug info
✓ Code reproduction
Usage Examples:
__str__ Usage:
print(car) # Calls __str__
str(car) # Explicit call
__repr__ Usage:
repr(car) # Explicit call
car # In interactive shell
Best Practices:
__str__:
Make it readable and user-friendly
__repr__:
Make it unambiguous and debuggable
Fallback:
If __str__ not defined, __repr__ is used
Goal:
eval(repr(obj)) should recreate the object