newbie: question about "__repr__"

John Roth newsgroups at jhrothjr.com
Wed Nov 5 21:53:24 EST 2003


"chenyu" <chenyu468 at hotmail.com> wrote in message
news:6143ac23.0311051814.4d9908f at posting.google.com...
> Hi,
> I have read others' code for study, and found one built-in function
> "__repr__". Could anyone give me an simple example for explaining
> "when and how" to use it.

__repr__ is the "magic method" that is used by the repr() built-in
function. It's similar to the str() built-in, but they serve different
purposes. str() is for when you want a human-readable representation,
repr() is for when you want more detail; most objects try to make whatever
repr() puts out round-trip through the exec statement and eval() functions.
This isn't always possible, but that's the difference between the two.

Both repr() and str() will use the other entry point if their prefered
magic method isn't availible.

John Roth
>
>
> Thank you for your attention.
> kind regards/chenyu






More information about the Python-list mailing list