Sebastian Witowski 1/25/2019

__str__ vs. __repr__

Read Original

This article clarifies the distinction between Python's __str__ and __repr__ magic methods. It explains that __str__ should be human-readable, while __repr__ should be an unambiguous string representation that ideally could recreate the object. The author uses practical examples, like datetime objects and a custom Car class, to illustrate how to implement __repr__ effectively and why this understanding is crucial for Python developers.

__str__ vs. __repr__

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week