By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Backticks: What up?

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Steven Brent wrote:
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

Jul 18 '05 #2

P: n/a
Backticks are equivalent to repr(). For any x:

`x` == repr(x)

Nothing to do with the return statement.

-- David Goodger
Jul 18 '05 #3

P: n/a
Steven Brent wrote:
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
Jul 18 '05 #4

P: n/a
Steven Brent wrote:
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
Jul 18 '05 #5

P: n/a
Thanks! I didn't know you could use printf type syntax with return....
Jul 18 '05 #6

P: n/a
Steven Brent wrote:
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:
def tuplify(a, b): .... 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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.