Backticks: What up?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Brent

    Backticks: What up?

    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.
  • donald.welch@hp.com

    #2
    Re: Backticks: What up?

    Steven Brent wrote:
    [color=blue]
    > 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.[/color]

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

    -Don

    Comment

    • David Goodger

      #3
      Re: Backticks: What up?

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

      `x` == repr(x)

      Nothing to do with the return statement.

      -- David Goodger


      Comment

      • Peter Hansen

        #4
        Re: Backticks: What up?

        Steven Brent wrote:
        [color=blue]
        > 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.[/color]

        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

        Comment

        • Peter Hansen

          #5
          Re: Backticks: What up?

          Steven Brent wrote:
          [color=blue]
          > 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.[/color]

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



          -Peter

          Comment

          • Steven Brent

            #6
            Re: Backticks: What up?

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

            Comment

            • Steve Holden

              #7
              Re: Backticks: What up?

              Steven Brent wrote:[color=blue]
              > Thanks! I didn't know you could use printf type syntax with return....[/color]

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

              regards
              Steve

              Comment

              Working...