Backticks: What up?

Peter Hansen peter at engcorp.com
Wed Apr 28 09:05:23 EDT 2004


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



More information about the Python-list mailing list