Backticks: What up?

S

Steven Brent

I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

TIA.
 
D

David Goodger

Backticks are equivalent to repr(). For any x:

`x` == repr(x)

Nothing to do with the return statement.

-- David Goodger
 
D

donald.welch

Steven said:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

TIA.

I think `self.data` is equivalent to repr(self.data).

-Don
 
P

Peter Hansen

Steven said:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

The above is equivalent to

return "Here's the result: %r" % self.data

Note that someone doing the `self.data` thing might really
have wanted to do the string representation instead (though
the two are often the same), so

return "Here's the result: %s" % self.data

would be more appropriate.

-Peter
 
P

Peter Hansen

Steven said:
I was wondering why the backticks in the following fragment:

return 'Here's the result: ' + `self.data`

My guess is that in a return statement (as opposed to a print statement)
it's necessary to do this in order to get the self.data instance attribute
as a string, so it can be concatenated with the 'Here's the result: '
string.

What exactly do the backticks do, then? Just return the result of an
expression as a string? Does my guess make sense and / or is it correct?
Elucidations and gentle ridicule welcome.

I was looking for the proper language ref but sometimes they
can be hard to track down:

http://docs.python.org/ref/string-conversions.html

-Peter
 
S

Steve Holden

Steven said:
Thanks! I didn't know you could use printf type syntax with return....

The "return" statement allows an arbitratu expression as its target,
even complex objects like sequences, classes and instances. For example:
.... return a, b
....
>>> tuplify([1, 2], [3, 4]) ([1, 2], [3, 4])
>>> thing, thong = tuplify("sing", (1, 2, 3))
>>> thing 'sing'
>>> thong (1, 2, 3)
>>>

regards
Steve
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top