Conversion to string: how do `s work?

C

Chris Lasher

I'm working my way through _Learning_Python_ 2nd ed., and I saw
something peculiar which is not explained anywhere in the text.

print " " + file + " size=" + `size`

The `s appear to somehow automagically convert the integer to a string
for concatenation. How does this work? Is this just a shortcut for
str(size)? Is it considered bad practice to use `s?
 
M

Michael Hoffman

Chris said:
The `s appear to somehow automagically convert the integer to a string
for concatenation. How does this work? Is this just a shortcut for
str(size)?

No, it's a shortcut for repr(size).
Is it considered bad practice to use `s?

Yes, please don't do it. I can't remember ever seeing it in production
code, so many people wouldn't even know what it was. And ' vs. ` is
confusing.
 
C

Chris Lasher

Ah, repr. Did not cross my mind. Good to see my suspicions about the
use of ` being poor practice were correct. "Explicit is better than
implicit."

Thanks for the reply.
 

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

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,756
Latest member
JulienneY0

Latest Threads

Top