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 it! --

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 why

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 d {0: repr} So if __str__

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 meant for human eyes,

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

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 container, why wouldn't it recursively

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Jean-Paul Calderone
On Thu, 18 Dec 2008 09:51:01 -0500, Neal Becker ndbeck...@gmail.com 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 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 sense to me. If I call 'print' on a container, why

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 to print. T. That makes no sense to me. If I call

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 dict just uses repr() for all its childs to print. T. That makes no

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__ =

Re: confused about __str__ vs. __repr__

2008-12-18 Thread rdmurray
Quoth Diez B. Roggisch de...@nospam.web.de: 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

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 x = X() d = {0 : x} print d {0:

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): return repr x = X() d = {0 : x}

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): def __str__(self): return str

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 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

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 def __repr__(self): return repr x =

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Gabriel Genellina
En Thu, 18 Dec 2008 14:05:32 -0200, Mikael Olofsson mik...@isy.liu.se 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

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,

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 mik...@isy.liu.se 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'

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 Russ P.
On Dec 18, 1:27 pm, Robert Kern robert.k...@gmail.com 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,

Re: confused about __str__ vs. __repr__

2008-12-18 Thread Carl Banks
On Dec 18, 9:53 am, Diez B. Roggisch de...@nospam.web.de 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:   print x str but