Re: %r
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: %r

From: Blackbird <fake@nospam.no>
Date: Mon Mar 06 2006 - 19:24:26 CET

Peter Hansen wrote:
> Blackbird wrote:
>> I'm trying to get a complete grip on %r. Is it true that the two
>> programs
>>
>> a = '*anything the parser accepts*'
>> print '%r' % a
>>
>> vs.
>>
>> a = r'*anything the parser accepts*'
>> print "'%s'" % a
>>
>> always produce the same output, where *anything the parser accepts*
>> can be replaced with, well, anything the parser accepts?
>
> "Always produce the same output?" Well, hardly, as even a token test
> proves:
>
> >>> a = 'testing\'this'
> >>> print '%r' % a
> "testing'this"
> >>> b = r'testing\'this'
> >>> print "'%s'" % b
> 'testing\'this'
>
>
> Do you realize that '%r' % a just outputs whatever repr(a) returns?
> And that '%s' % a just outputs whatever str(a) returns?
>
> -Peter

I got it now (retrospectively, this turned out be an exercise in 'posting
before thinking'). Since many different looking string literals will
produce the same string, obviously my question must be answered in the
negative. It's of course

a = 'something'
b = ('%r'%a)
eval(b) == a

that in general should return True. I hope. But I'm sure there are
counterexamples to that, too.
Received on Sun Apr 30 10:59:59 2006