what's the use of __repr__?when shall I use it?

C

could ildg

What's the difference between __repr__ and __str__?
When will __repr__ be useful?
 
G

Greg Ewing

Vikram said:
__repr__ should return something that when eval'ed yields an identical
object (if possible).

That's strictly possible in so few cases that it's not
really a very helpful guideline, in my opinion.

I find the following view more helpful:

* str() is for producing the normal output of a program,
to be seen by the user.

* repr() is for debugging output, and should indicate
reasonably unambiguously the *type* of the object.
When debugging, it's often at least as important to
know what type of object you have as what value it
has.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top