%r

B

Blackbird

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

Duncan Booth

Blackbird said:
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?
'I don\'t think so'
 
F

Fredrik Lundh

Blackbird said:
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?

no.

%s does a str() on the object (if it's not already a string), while
%r does a repr(). the raw string syntax has nothing to do with
this.

(see the builtin function section in the library reference for more
on str() and repr())

</F>
 
P

Peter Hansen

Blackbird said:
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:
'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
 
F

Fredrik Lundh

Blackbird said:
a = 'I don\'t think so'
print '%r' % a "I don't think so"
a = r'I don\'t think so'
print "'%s'" % a
'I don\'t think so'

Excellent counterexample. Can something like this happen for other things
than quotes?
'\377'

I still recommend looking up %s and %r and str() and repr() in the
documentation. it's not hard to understand what they do, and that
approach works a bit better than cargo cult programming.

</F>
 
B

Blackbird

Peter said:
"Always produce the same output?" Well, hardly, as even a token test
proves:

'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.
 
P

Peter Hansen

Blackbird said:
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.

Provided 'something' is a string, that should be true, as should the
simpler statement a == eval(repr(a)) (since '%r' is just a more complex
way of getting the same thing as repr()).

On the other hand, if 'something' is actually some arbitrary object,
then it's definitely not always true, especially for anything not built
in to Python.

-Peter
 
B

Blackbird

Fredrik said:
Blackbird said:
[...]

a = 'I don\'t think so'
print '%r' % a
"I don't think so"
a = r'I don\'t think so'
print "'%s'" % a
'I don\'t think so'

Excellent counterexample. Can something like this happen for other
things than quotes?
a = '\377'
print '%r' % a '\xff'
a = r'\377'
print "'%s'" % a
'\377'

I still recommend looking up %s and %r and str() and repr() in the
documentation. it's not hard to understand what they do, and that
approach works a bit better than cargo cult programming.

By "cargo cult programming", do you mean actually *running* the code?
Noooo, no, no. I *write* code. I don't *run* code any longer. A long life
has thought me that the awakenings you are in for, by running code, are rude
and only rude.

Blackbird
 
B

Blackbird

Peter said:
[...]>
On the other hand, if 'something' is actually some arbitrary object,
then it's definitely not always true, especially for anything not
built in to Python.

-Peter

Yes, I had strings in mind. And I guess it will work with the other
primitive types, but anything that repr to something like '<function f at
0x0128CFB0>' will crash, since it's not a valid Python expression.

Is the interpreter in fact using repr(), or parts of it, to generate output
when you type an expression at the command prompt? The results sure look
similar.

Blackbird
 
B

Blackbird

Blackbird said:
Is the interpreter in fact using repr(), or parts of it, to generate
output when you type an expression at the command prompt? The
results sure look similar.

Let me clarify this: The interpreter uses eval. No doubt about that. But
'<function f at 0x0128CFB0>'

So there is still a connection between eval and repr that I don't fully
understand, besides the fact that eval (among other things) is the inverse
of repr. Do they share some code, and if so, what part? Well, I guess I
could of course take a few weeks off and dive into the source... not sure my
wife would approve..
 
B

Blackbird

Blackbird said:
Let me clarify this: The interpreter uses eval. No doubt about
that. But

'<function f at 0x0128CFB0>'

So there is still a connection between eval and repr that I don't
fully understand, besides the fact that eval (among other things) is
the inverse of repr. Do they share some code, and if so, what part?
Well, I guess I could of course take a few weeks off and dive into
the source... not sure my wife would approve..


Aarghh... the command line interpreter of course use both repr and eval.
In fact, when presented by a top level (unindented) expression,

<expr>

and

print repr(eval('<expr>'))

the two lines produce the same output, so this last program *is* in fact the
interpreter. Now I'll get some sleep, and a life, and stop talking to
myself on usenet.
 
F

Fredrik Lundh

Blackbird said:
By "cargo cult programming", do you mean actually *running* the code?

no, I mean basing your mental model of something on distant observations
of superficial (or accidental) artifacts (like the perceived similarity between
the output from repr() and the raw string literal syntax), rather than on an
attempt to understand what's really going on, and what causes what.

or something like that.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,448
Members
48,126
Latest member
ToneyChun2

Latest Threads

Top