On Wed, 17 Dec 2008 15:25:41 -0800, Chris Rebert wrote:
> 2008/12/17 Li Han <[email protected]>:
>> On 12月18日, 上午7时12分, Scott David Daniels <[email protected]>
wrote:
>> Scott wrote:
>>> Try: print repr(repr("'"))
>>> that might enlighten you.
>>
>> I found that print( repr( repr( arbitarystring ) ) ) == repr (
>> arbitarystring )
>
> As I stated previously, the key rule is:
>
> eval(repr(something)) == something
>
> That is, repr() gives a string of Python code that, when evaluated,
> results in what you gave to repr().
That is not true in general.
>>> from urllib2 import HTTPError
>>> h = HTTPError("404 quoth the Raven", None, None, None, None)
>>> eval(repr(h))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: __init__() takes exactly 6 arguments (1 given)
Even for built-ins, it's not always true:
>>> eval(repr(float('inf')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'inf' is not defined
In other words, there is no guarantee that repr(obj) will round-trip
correctly, or at all. It's a Nice To Have, and it will often work, but
it's not something you should rely on for arbitrary objects.
However, I believe it is true for strings.
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list