what does ` ` do in ' '.join([`x * x` for x in range(1, 6)])?

P

process

' '.join([`x * x` for x in range(1, 6)])

exactly what does this symbol do and what does it stand for?
 
A

alex23

' '.join([`x * x` for x in range(1, 6)])

exactly what does this symbol do and what does it stand for?

`<object>` is the same as repr(<object>). I'm pretty sure the backtick
operator has been removed from 3.0.

In the context of the code sample you posted, it appears to be used to
convert the result of x*x into a string for the join method, but I
personally think it's a misuse of repr() and think a call to str()
would be more appropriate:
'1 4 9 16 25'
 
R

r0g

process said:
' '.join([`x * x` for x in range(1, 6)])

exactly what does this symbol do and what does it stand for?


Which symbol, the '*' ???

Are you kidding?
 
R

r0g

process said:
' '.join([`x * x` for x in range(1, 6)])

exactly what does this symbol do and what does it stand for?

Ah, just spotted the backticks - they just return whatever's inside them
as a string.
 
A

alex23

Ah, just spotted the backticks - they just return whatever's inside them
as a string.

No, they return the repr() of the object inside. The output of
__repr__ -has- to be a string, but typecasting into string isn't the
intention of repr() (or the ` operator).
 
R

Roy Smith

alex23 said:
' '.join((str(x * x) for x in range(1,6)))

Aren't the outer set of parens redundant? This works just as well:

' '.join(str(x * x) for x in range(1,6))
 
M

MRAB

process said:
' '.join([`x * x` for x in range(1, 6)])
exactly what does this symbol do and what does it stand for?

It's an obsolete, deprecated syntactic sugar for (what is now
implemented as) the built-in 'repr' function.

Instead, write the above as:

    ' '.join([repr(x * x) for x in range(1, 6)])
I'd classify it as one of Guido's mistakes! :)
 

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,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top