Re: confused about __str__ vs. __repr__

2008-12-18 Thread Carl Banks
On Dec 18, 9:53 am, "Diez B. Roggisch" wrote: > Neal Becker wrote: > > Diez B. Roggisch wrote: > > >> Neal Becker wrote: > > >>> Tino Wildenhain wrote: > > Neal Becker wrote: > ... > >>> So if __str__ is "meant for human eyes", then why isn't print using > >>> it! > >> it is:

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Russ P.
On Dec 18, 1:27 pm, Robert Kern wrote: > Mikael Olofsson wrote: > > Diez B. Roggisch wrote: > >> Yep. And it's easy enough if you don't care about them being different.. > > >> def __repr__(self): > >>     return str(self) > > > If I ever wanted __str__ and __repr__ to return the same thing, I wou

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Steven D'Aprano wrote: > BTW Neal, your posts aren't word wrapped. When I read your posts, I get > each paragraph as one extremely LONG line scrolling way out to the side. > That's against the Internet standards for both email and Usenet, so could > you please configure your client to word-wrap at

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Steven D'Aprano
On Thu, 18 Dec 2008 22:11:27 -0200, Gabriel Genellina wrote: > En Thu, 18 Dec 2008 14:05:32 -0200, Mikael Olofsson > escribió: ... >> If I ever wanted __str__ and __repr__ to return the same thing, I would >> make them equal: >> >> def __str__(self): >> return 'whatever you want' >> __repr__

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Steven D'Aprano
On Thu, 18 Dec 2008 10:49:27 -0500, Neal Becker wrote: > So if I want to overload something in my custom class, so that I get a > nice string whether it's printed directly, or as part of a container, > what is the recommendation? Overload both __str__ and __repr__? Either or both or neither, wha

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Gabriel Genellina
En Thu, 18 Dec 2008 14:05:32 -0200, Mikael Olofsson escribió: Diez B. Roggisch wrote: Yep. And it's easy enough if you don't care about them being different.. def __repr__(self): return str(self) If I ever wanted __str__ and __repr__ to return the same thing, I would make them equal:

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Steve Holden
Neal Becker wrote: > Mel wrote: > >> Neal Becker wrote: >> >>> Tino Wildenhain wrote: >>> Neal Becker wrote: > Reading some FAQ, I see that __str__ is "meant for human eyes". > > But it seems that: > class X(object): > def __str__(self): > return "str"

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Scott David Daniels
J. Cliff Dyer wrote: ... how an object prints itself is up to that object and that object alone If I wanted to implement a list-like class that doesn't show it's elements at > all when printed, but instead shows its length, I am free to do so. For example: hl = HiddenList(1,2,3) hl hl

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Robert Kern
Mikael Olofsson wrote: Diez B. Roggisch wrote: Yep. And it's easy enough if you don't care about them being different.. def __repr__(self): return str(self) If I ever wanted __str__ and __repr__ to return the same thing, I would make them equal: def __str__(self): return 'whatever

Re: confused about __str__ vs. __repr__

2008-12-18 Thread J. Cliff Dyer
On Thu, 2008-12-18 at 13:35 -0500, Neal Becker wrote: > Mel wrote: > > > Neal Becker wrote: > > > >> Tino Wildenhain wrote: > >> > >>> Neal Becker wrote: > Reading some FAQ, I see that __str__ is "meant for human eyes". > > But it seems that: > class X(object): > d

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Mel wrote: > Neal Becker wrote: > >> Tino Wildenhain wrote: >> >>> Neal Becker wrote: Reading some FAQ, I see that __str__ is "meant for human eyes". But it seems that: class X(object): def __str__(self): return "str" def __repr__(self):

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Mel
Neal Becker wrote: > Tino Wildenhain wrote: > >> Neal Becker wrote: >>> Reading some FAQ, I see that __str__ is "meant for human eyes". >>> >>> But it seems that: >>> class X(object): >>> def __str__(self): >>> return "str" >>> def __repr__(self): >>> return "repr" >>> >

Re: confused about __str__ vs. __repr__

2008-12-18 Thread rdmurray
Quoth "Diez B. Roggisch" : > Neal Becker wrote: > > > Tino Wildenhain wrote: > > > >> Neal Becker wrote: > >> ... > >>> That makes no sense to me. If I call 'print' on a container, why > >>> wouldn't it recursively print on the contained objects? Since print > >>> means call str, printing a co

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Mikael Olofsson
Diez B. Roggisch wrote: Yep. And it's easy enough if you don't care about them being different.. def __repr__(self): return str(self) If I ever wanted __str__ and __repr__ to return the same thing, I would make them equal: def __str__(self): return 'whatever you want' __repr__ = __s

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Diez B. Roggisch
Neal Becker wrote: > Diez B. Roggisch wrote: > >> Neal Becker wrote: >> >>> Tino Wildenhain wrote: >>> Neal Becker wrote: ... >>> So if __str__ is "meant for human eyes", then why isn't print using >>> it! >> it is: >> >> > print x >> str >> >> but dic

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Diez B. Roggisch wrote: > Neal Becker wrote: > >> Tino Wildenhain wrote: >> >>> Neal Becker wrote: >>> ... >> So if __str__ is "meant for human eyes", then why isn't print using >> it! > it is: > > > print x > str > > but dict just uses repr() for all its childs

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Diez B. Roggisch
Neal Becker wrote: > Tino Wildenhain wrote: > >> Neal Becker wrote: >> ... > So if __str__ is "meant for human eyes", then why isn't print using > it! it is: > print x str but dict just uses repr() for all its childs to print. T. >>> That makes no

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Jean-Paul Calderone
On Thu, 18 Dec 2008 09:51:01 -0500, Neal Becker wrote: Tino Wildenhain wrote: Neal Becker wrote: ... So if __str__ is "meant for human eyes", then why isn't print using it! it is: > print x str but dict just uses repr() for all its childs to print. T. That makes no sense to me. If I ca

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Tino Wildenhain wrote: > Neal Becker wrote: > ... So if __str__ is "meant for human eyes", then why isn't print using it! >>> it is: >>> >>> > print x >>> str >>> >>> but dict just uses repr() for all its childs to print. >>> >>> T. >> That makes no sense to me. If I call 'print' on a conta

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Tino Wildenhain
Neal Becker wrote: ... So if __str__ is "meant for human eyes", then why isn't print using it! it is: > print x str but dict just uses repr() for all its childs to print. T. That makes no sense to me. If I call 'print' on a container, why wouldn't it recursively print on the contained ob

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Diez B. Roggisch
Neal Becker wrote: > Reading some FAQ, I see that __str__ is "meant for human eyes". > > But it seems that: > class X(object): >     def __str__(self): >         return "str" >     def __repr__(self): >         return "repr" > > x = X() > d = {0 : x} > print d > {0: repr} > > So if __str__ is "

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Tino Wildenhain wrote: > Neal Becker wrote: >> Reading some FAQ, I see that __str__ is "meant for human eyes". >> >> But it seems that: >> class X(object): >> def __str__(self): >> return "str" >> def __repr__(self): >> return "repr" >> >> x = X() >> d = {0 : x} >> print

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Tino Wildenhain
Neal Becker wrote: Reading some FAQ, I see that __str__ is "meant for human eyes". But it seems that: class X(object): def __str__(self): return "str" def __repr__(self): return "repr" x = X() d = {0 : x} print d {0: repr} So if __str__ is "meant for human eyes", then w

confused about __str__ vs. __repr__

2008-12-18 Thread Neal Becker
Reading some FAQ, I see that __str__ is "meant for human eyes". But it seems that: class X(object): def __str__(self): return "str" def __repr__(self): return "repr" x = X() d = {0 : x} print d {0: repr} So if __str__ is "meant for human eyes", then why isn't print using

Re: FAQ: __str__ vs __repr__

2005-06-21 Thread Skip Montanaro
>> __repr__ shouldn't be anything, if you don't have an actual need for >> it. Neither should __str__. Simon> Oh, I don't know. __str__ is so frequently useful in debugging Simon> and logging that I always try and do something useful with it. And sometimes __repr__ inherited fro

Re: FAQ: __str__ vs __repr__

2005-06-21 Thread Peter Hansen
Simon Brunning wrote: > On 6/15/05, Peter Hansen <[EMAIL PROTECTED]> wrote: >>__repr__ shouldn't be anything, if you don't have an actual need for it. >> Neither should __str__. > > Oh, I don't know. __str__ is so frequently useful in debugging and > logging that I always try and do something use

Re: FAQ: __str__ vs __repr__

2005-06-21 Thread Simon Brunning
On 6/15/05, Peter Hansen <[EMAIL PROTECTED]> wrote: > __repr__ shouldn't be anything, if you don't have an actual need for it. > Neither should __str__. Oh, I don't know. __str__ is so frequently useful in debugging and logging that I always try and do something useful with it. -- Cheers, Simo

Re: __str__ vs __repr__

2005-06-16 Thread Donn Cave
In article <[EMAIL PROTECTED]>, "John Roth" <[EMAIL PROTECTED]> wrote: > "Donn Cave" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Quoth "John Roth" <[EMAIL PROTECTED]>: > > ... > > | str() should be something that's meaningful to a human being when > > | it's printed or othe

Re: __str__ vs __repr__

2005-06-16 Thread John Roth
"Donn Cave" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Quoth "John Roth" <[EMAIL PROTECTED]>: > ... > | str() should be something that's meaningful to a human being when > | it's printed or otherwise rendered. > > I can't believe how many people cite this explanation - meaningf

Re: __str__ vs __repr__

2005-06-15 Thread Donn Cave
Quoth "John Roth" <[EMAIL PROTECTED]>: ... | str() should be something that's meaningful to a human being when | it's printed or otherwise rendered. I can't believe how many people cite this explanation - meaningful, friendly, pretty to a human being. What on earth does this mean, that couldn't b

Re: __str__ vs __repr__

2005-06-15 Thread John Roth
"Jan Danielsson" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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: > > class Serv

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Dave Benjamin
Jan Danielsson wrote: > 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. Say we define a string "s" as follows: >>> s = 'hello' If we print "s", we see its string form (__str__): >>> print s hello However, if we just examine "s",

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Terry Hancock
On Wednesday 15 June 2005 08:06 am, Sébastien Boisgérault wrote: > Jan Danielsson a écrit : > >However, I don't understand what __repr__ should be. > It is an *exact* (if possible) description of the object's content, > nicely packaged into a string. However, this is often not the case. Freque

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Donn Cave
In article <[EMAIL PROTECTED]>, Jan Danielsson <[EMAIL PROTECTED]> wrote: > 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. No, it is not. It is a con

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Peter Hansen
Jan Danielsson wrote: > 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. [snip] > However, I don't understand what __repr__ should be. __repr__ shouldn't b

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Greg Krohn
Basically __repr__ should return a string representaion of the object, and __str__ should return a user-friendly pretty string. So maybe: class Person: ... def __repr__(self): return '' % (self.name, self.age, self.sign) def __str__(self): return self.name See this for a bett

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread Andreas Kostyrka
Well, It means that eval(repr(x)) == x if at all possible. Basically: repr('abc') -> 'abc' str('abc') -> abc You'll notice that 'abc' is a valid python expression for the string, while abc is not a valid string expression. Andreas On Wed, Jun 15, 2005 at 02:46:04PM +0200, Jan Danielsson wrote: >

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=
Errata: >>> str(0.1) '0.1' >>> str("it's a bad idea") "it's a bad idea" >>> repr(0.1) ' 0.10001' >>> repr("it's a bad idea") '"it\'s a bad idea"' SB -- http://mail.python.org/mailman/listinfo/python-list

Re: FAQ: __str__ vs __repr__

2005-06-15 Thread =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=
Jan Danielsson a écrit : > 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. ... yep, and this representation is built for human eyes. Don't worry too much if

FAQ: __str__ vs __repr__

2005-06-15 Thread Jan Danielsson
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: class ServerConnection: def __str__(self): buf = "Server: " + self.name + "\n" buf +