Multiple assignment and the expression on the right side

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Suresh Jeevanandam

    #1

    Multiple assignment and the expression on the right side

    Dear all,
    I read in "Python in a Nutshell" that when we have multiple assignments
    made on a single line, it is equivalent to have those many simple
    assignments and that the right side is evaluated once for each
    assignment. [The wordings are mine. I am not sure if this is what he
    intended].

    So, In the following code snippet I expected the values of c, d, e to
    be different. But they are not? I am missing something... What is it?[color=blue][color=green][color=darkred]
    >>> a = 12
    >>> def x(): global a; a+=1; return(a)[/color][/color][/color]
    ....[color=blue][color=green][color=darkred]
    >>> x()[/color][/color][/color]
    13[color=blue][color=green][color=darkred]
    >>> a[/color][/color][/color]
    13[color=blue][color=green][color=darkred]
    >>> c = d = e = x()
    >>> c[/color][/color][/color]
    14[color=blue][color=green][color=darkred]
    >>> d[/color][/color][/color]
    14[color=blue][color=green][color=darkred]
    >>> e[/color][/color][/color]
    14[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Thanks.
    regards,
    Suresh
  • Robert Kern

    #2
    Re: Multiple assignment and the expression on the right side

    Suresh Jeevanandam wrote:[color=blue]
    > Dear all,
    > I read in "Python in a Nutshell" that when we have multiple assignments
    > made on a single line, it is equivalent to have those many simple
    > assignments and that the right side is evaluated once for each
    > assignment. [The wordings are mine. I am not sure if this is what he
    > intended].[/color]

    No, it isn't. He says, "Each time the statement executes, the right-hand side
    expression is evaluated once." "[T]he statement" refers to the multiple
    assignment as a whole.

    --
    Robert Kern
    robert.kern@gma il.com

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Alex Martelli

      #3
      Re: Multiple assignment and the expression on the right side

      Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
      [color=blue]
      > Dear all,
      > I read in "Python in a Nutshell" that when we have multiple assignments
      > made on a single line, it is equivalent to have those many simple
      > assignments and that the right side is evaluated once for each
      > assignment. [The wordings are mine. I am not sure if this is what he
      > intended].[/color]

      Since the original text is:

      """
      Each time the statement executes, the right-hand side expression is
      evaluated once. Each target gets bound to the single object returned by
      the expression.
      """

      it might be interesting to understand how you managed to translate ONCE
      into ONCE PER ASSIGNMENT TARGET. I can most earnestly assure you that
      when I wrote ONCE I meant ONCE. If ONCE meant TWICE (or more), how could
      there be a guaranteed SINGLE OBJECT to which each target gets bound?
      [color=blue]
      > So, In the following code snippet I expected the values of c, d, e to
      > be different. But they are not? I am missing something... What is it?[/color]

      A good command of English, maybe? With that ONCE and SINGLE in there,
      no matter how much I try to, I just cannot see ambiguity in the words I
      had written. Nevertheless, in your honor, I guess I will redundantly
      change the ONCE into JUST ONCE in the second edition (I do strive for
      maximum conciseness in the Nutshell, but I guess I can spare one extra
      four-letter word, even though this is the only time I ever heard anybody
      express any doubt or misunderstandin g about this paragraph).


      Alex

      Comment

      • Andrea Griffini

        #4
        Re: Multiple assignment and the expression on the right side

        While I think that the paragraph is correct still there is IMO indeed
        the (low) risk of such a misunderstandin g. The problem is that "the
        statement executes" can IMO easily be understood as "the statements
        execute" (especially if your background includes only languages where
        there's no multiple assignment) and the world "single" is also
        frequently used in phrases like "every single time" where can indeed
        denote a context of plurality.
        Adding "just" or "only" would be IMO an great improvement by focusing
        the attention on the key point, as it would be IMO better using
        "rightmost" instead of "right-hand" (in this case even saving a char
        ;-) )

        Just two foreign cents

        Comment

        • Suresh Jeevanandam

          #5
          Re: Multiple assignment and the expression on the right side

          Alex Martelli wrote:[color=blue]
          > Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
          >[color=green]
          >> Dear all,
          >> I read in "Python in a Nutshell" that when we have multiple assignments
          >> made on a single line, it is equivalent to have those many simple
          >> assignments and that the right side is evaluated once for each
          >> assignment. [The wordings are mine. I am not sure if this is what he
          >> intended].[/color]
          >
          > Since the original text is:
          >
          > """
          > Each time the statement executes, the right-hand side expression is
          > evaluated once. Each target gets bound to the single object returned by
          > the expression.
          > """[/color]

          Alex,
          I should have read carefully.

          I think I got confused because of "Each time" in the sentence which
          gives a feeling that it gets executed several times. Maybe, It could
          have been just written, "When the statement gets executed, the right
          hand side is evaluated once, and the result is assigned to each of the
          target".

          Thanks a lot.

          regards,
          Suresh
          [color=blue]
          > it might be interesting to understand how you managed to translate ONCE
          > into ONCE PER ASSIGNMENT TARGET. I can most earnestly assure you that
          > when I wrote ONCE I meant ONCE. If ONCE meant TWICE (or more), how could
          > there be a guaranteed SINGLE OBJECT to which each target gets bound?
          >[color=green]
          >> So, In the following code snippet I expected the values of c, d, e to
          >> be different. But they are not? I am missing something... What is it?[/color]
          >
          > A good command of English, maybe? With that ONCE and SINGLE in there,
          > no matter how much I try to, I just cannot see ambiguity in the words I
          > had written. Nevertheless, in your honor, I guess I will redundantly
          > change the ONCE into JUST ONCE in the second edition (I do strive for
          > maximum conciseness in the Nutshell, but I guess I can spare one extra
          > four-letter word, even though this is the only time I ever heard anybody
          > express any doubt or misunderstandin g about this paragraph).
          >
          >
          > Alex[/color]

          Comment

          • Alex Martelli

            #6
            Re: Multiple assignment and the expression on the right side

            Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:
            ...[color=blue]
            > I think I got confused because of "Each time" in the sentence which
            > gives a feeling that it gets executed several times. Maybe, It could
            > have been just written, "When the statement gets executed, the right
            > hand side is evaluated once, and the result is assigned to each of the
            > target".[/color]

            Ah, but that suggests to *ME* that if you have the statement in a loop,
            the RHS executes only once (the first time). Let's try:

            """
            Each time the statement executes, the RHS expression is evaluated just
            once, no matter how many targets are part of the statement. Each target
            then gets bound to the single object returned by the expression, just as
            if several simple assignments executed one after the other.
            """

            [[I'm using LHS and RHS in the 2nd edition, for extra conciseness, after
            introducing the acronyms just once where I first use them]].

            This seems to cover the bases:

            a. RHS is evaluated EACH TIME the statement executes,
            b. but for each such time, RHS is evaluated JUST ONCE.

            and the addition of the "no matter" clause should pound the nail into
            the coffin. What do y'all think?


            Thanks for the comments, by the way, and my apologies for reacting to
            them rather snappily at first blush -- I had just spent a long weekend
            mostly slaving on the 2nd edition, so I guess I was irritable, but
            that's no excuse for my discourtesy: sorry. Comments and criticisms on
            my work are in fact always welcome (submitting them as errata on
            O'Reilly's site ensures I will see them, which isn't certain here),
            since they give me the best chance to enhance the work's quality (it's
            quite common for the author of some text to fail to see potential issues
            with the text, which may trip some readers but appears to be perfect to
            the author -- that's why two heads are better than one, and N>2 even
            better than two!-).


            So, again, thanks.

            Alex

            Comment

            • Terry Hancock

              #7
              Re: Multiple assignment and the expression on the right side

              On Tue, 21 Feb 2006 10:53:21 +0530
              Suresh Jeevanandam <jm.suresh@gmai l.com> wrote:[color=blue]
              > I read in "Python in a Nutshell" that when we have
              > multiple assignments
              > made on a single line, it is equivalent to have those many
              > simple assignments and that the right side is evaluated
              > once for each assignment. [The wordings are mine. I am
              > not sure if this is what he intended].[/color]
              [color=blue][color=green][color=darkred]
              > >>> c = d = e = x()[/color][/color][/color]

              AFAIK, this is equivalent to this:

              e = x()
              d = e
              c = d

              So, in fact, what you say is true, but these, of course will
              not evaluate x multiple times.

              --
              Terry Hancock (hancock@Anansi Spaceworks.com)
              Anansi Spaceworks http://www.AnansiSpaceworks.com

              Comment

              Working...