list assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Norvell Spearman

    #1

    list assignment

    In "Learning Python," by Lutz and Ascher, there's a table showing different
    assignment statement forms. One form shown is list assignment. The authors
    give this as an example:

    [spam, ham] = ['yum', 'YUM']

    I don't see how this is any different than a tuple unpacking assignment:
    [color=blue][color=green][color=darkred]
    >>> a, b = 1, 2
    >>> a, b[/color][/color][/color]
    (1, 2)[color=blue][color=green][color=darkred]
    >>> [a, b] = [1, 2]
    >>> a, b[/color][/color][/color]
    (1, 2)

    In both instances the names a and b are both mapped to 1 and 2 so why are there
    two different forms?

    Thanks for any answers.

    --
    Norvell Spearman
  • Raymond Hettinger

    #2
    Re: list assignment

    > [spam, ham] = ['yum', 'YUM'][color=blue]
    >
    > I don't see how this is any different than a tuple unpacking assignment:
    >[color=green][color=darkred]
    > >>> a, b = 1, 2[/color][/color][/color]

    It's not different. They are ways of writing the same thing.


    Raymond Hettinger

    Comment

    • Jeffrey Schwab

      #3
      Re: list assignment

      Raymond Hettinger wrote:[color=blue][color=green]
      >> [spam, ham] = ['yum', 'YUM']
      >>
      >>I don't see how this is any different than a tuple unpacking assignment:
      >>[color=darkred]
      >> >>> a, b = 1, 2[/color][/color]
      >
      >
      > It's not different. They are ways of writing the same thing.[/color]

      TMTOWTDI, after all. :)

      Comment

      • Norvell Spearman

        #4
        Re: list assignment

        Raymond Hettinger wrote:[color=blue]
        > It's not different. They are ways of writing the same thing.[/color]

        Lutz and Ascher have tuple and list assignment as separate entries in their
        assignment statement forms table so I was expecting there to be some
        difference; thanks for setting me straight.

        --
        Norvell Spearman

        Comment

        • Norvell Spearman

          #5
          Re: list assignment

          Jeffrey Schwab wrote:[color=blue]
          > TMTOWTDI, after all. :)[/color]

          A bit ironic that that's the official motto of Perl, don't you think?

          --
          Norvell Spearman

          Comment

          • Bernhard Herzog

            #6
            Re: list assignment

            Norvell Spearman <norvell@stenoc all.com> writes:
            [color=blue]
            > Lutz and Ascher have tuple and list assignment as separate entries in
            > their assignment statement forms table so I was expecting there to be
            > some difference; thanks for setting me straight.[/color]

            In older Python versions there was a difference between list unpacking
            and tuple unpacking. The former would only work with lists and the
            latter with tuples. With Python 1.5, both were unified into a more
            general sequence unpacking, but for backwards compatibility both
            syntaxes were kept.

            Bernhard

            --
            Intevation GmbH http://intevation.de/
            Skencil http://skencil.org/
            Thuban http://thuban.intevation.org/

            Comment

            Working...