473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

What's the difference between __repr__ and __str__?
When will __repr__ be useful?
Jul 18 '05 #1
2 2989

__repr__ should return something that when eval'ed yields an identical
object (if possible). essentially it should represent the programmer/debug
view of the object.

__str__ should mostly return a highly user-friendly string (note: str(obj)
will use __repr__ if __str__ doesn't exist)

you might also be interested in this thread covering the same topic:
http://groups-beta.google.com/group/...8f62f478057abd

thanks,
Vikram

Jul 18 '05 #2
Vikram wrote:
__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.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Sommers | last post by:
Hi, I have a class whose objects represent physical quantities including uncertainties and units, and I would like more control over the way they print. I have a __str__ method which outputs...
15
by: Jan Danielsson | last post by:
Sorry, but I Just Don't Get It. I did search the 'net, I did read the FAQ, but I'm too dumb to understand. As far as I can gather, __str__ is just a representation of the object. For instance: ...
8
by: Claudio Grondi | last post by:
It is maybe not a pure Python question, but I think it is the right newsgroup to ask for help, anyway. After connecting a drive to the system (via USB or IDE) I would like to be able to see...
7
by: Ben Finney | last post by:
Howdy all, The builtin types have __repr__ attributes that return something nice, that looks like the syntax one would use to create that particular instance. The default __repr__ for custom...
5
by: consternation | last post by:
I can't find neither in tutorial nor with google It's all about isinstance, or __class__. How to test that an object is an instance of my X class?? Do I have this problems because I stre my...
1
by: Martin Drautzburg | last post by:
I am using repr() to pass arrays, dicts and combinations therof to javascript as it already returns a valid javascript expression (a string) right away. But for some reason it does not handle...
0
by: Roc Zhou | last post by:
Now I have to design a class that overload __getattr__, but after that, I found the __repr__ have been affected. This is a simple example model: #!/usr/bin/env python class test: def...
7
by: Roc Zhou | last post by:
Now I have to design a class that overload __getattr__, but after that, I found the __repr__ have been affected. This is a simple example model: #!/usr/bin/env python class test: def...
0
by: Terry Reedy | last post by:
srinivasan srinivas wrote: When posting problems like this, please include the Python version. If you ran this with 2.6, please run the following: x = frozenset('abc') y = frozenset('bcd') z...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.