From: Christopher B. <Chr...@no...> - 2007-12-13 23:13:45
|
Gael Varoquaux wrote: >> x == eval(repr(x)) > >> That is true for many of the builtin data types of the language. And the really the whole point of having __repr__, in addition to __str__ > I totally agree. However if a user types: > pylab.rcParams > in IPython, or the Python interpreter, she gets the repr, AFAIK. That's a python interpreter change a couple versions back. One of the reasons for it is: >>> str(a) '0.1' >>> str(b) '0.1' >>> a == b False huh? why false??? >>> a 0.10000000000000002 >>> b 0.10000000000000001 >>> Ah -- now I see. It was felt that this was a case where being as precise as possible with the default output was a good idea. If you want the pretty version, use print: >>> print a, b 0.1 0.1 I'm not up on the details of this specific issue, but in general, the idea that: __repr__ is precise and complete __str__ is pretty and readable is a good one. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |