multiple assignment

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

    #1

    multiple assignment


    Suppose i have a big list and i want to take tke the first one and rest
    of the list like car/cdr in lisp.
    is there any easy way to do this in python?

    Only way i know is

    a = range(10)
    x, y = a[0], a[1:]

    In perl, it is possible to do multiple assignment like this....

    @a = (1, 2, 3);
    ($x, @y) = @a;

    I find this is a good feature and missing in python.
    Why can't python support something like this:

    x, *y = a

    * operator is used in the usual sence here.

    This is not really a new concept to python, infact when calling a
    function which takes variable arguments, it is used in a similar way.
    for example:

    def f(x, *y): print x, y
    f(1, 2, 3)

    the argument passing here is very similar to the multiple assignment x,
    *y = (1, 2, 3)

    any comments?

    -anand

  • Christoph Zwerschke

    #2
    Re: multiple assignment

    Anand schrieb:[color=blue]
    > Suppose i have a big list and i want to take tke the first one and rest
    > of the list like car/cdr in lisp.
    > is there any easy way to do this in python?
    >
    > Only way i know is
    >
    > a = range(10)
    > x, y = a[0], a[1:][/color]

    You have so many higher-level ways to access and iterate through lists
    in Python, that you normally just don't need to do things like that.

    Also, in the frequent case where y=a, you can just write x = a.pop(0).
    [color=blue]
    > Why can't python support something like this:
    >
    > x, *y = a
    >
    > This is not really a new concept to python, infact when calling a
    > function which takes variable arguments, it is used in a similar way.[/color]

    You're right, that would not be so far off.
    But then, the following should be also supported:

    *x, y = a # x, y = a[:-1], y = a[-1]
    x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]

    Of course, there can be only one variable with an asterisk.
    (But note that in the situation of a function taking parameters, that
    variable must always be the last.)

    But I don't know if this is really useful enough...

    -- Christoph

    Comment

    • Fredrik Lundh

      #3
      Re: multiple assignment

      Christoph Zwerschke wrote:
      [color=blue]
      > You're right, that would not be so far off.
      > But then, the following should be also supported:
      >
      > *x, y = a # x, y = a[:-1], y = a[-1]
      > x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
      >
      > Of course, there can be only one variable with an asterisk.
      > (But note that in the situation of a function taking parameters, that
      > variable must always be the last.)
      >
      > But I don't know if this is really useful enough...[/color]

      things like this are proposed from time to time (I haven't looked, but
      there might even be a PEP somewhere). however, function calls and
      assignments are two different things, and I'm not convinced that it's
      not yet another hypergeneraliza tion...

      </F>



      Comment

      • Anand

        #4
        Re: multiple assignment

        > You're right, that would not be so far off.[color=blue]
        > But then, the following should be also supported:
        >
        > *x, y = a # x, y = a[:-1], y = a[-1]
        > x, *y, z = a # x, y, z = a[0], a[1:-1], a[-1]
        >
        > Of course, there can be only one variable with an asterisk.
        > (But note that in the situation of a function taking parameters, that
        > variable must always be the last.)[/color]

        Same argument can be applied for functions also. whats wrong in having
        some thing like this?

        def f(x, *y, z): pass

        I think there is a problem in both these cases.
        [color=blue]
        > But I don't know if this is really useful enough...[/color]

        I think it is really useful.
        One which i encountered was there is a file where each line has tokens
        separated by commas. First token is the id and i want to use it in a
        special way.

        Wouldn't it be nice to say

        id, *tokens = line.split(',')

        than

        tokens = line.split(',')
        id = tokens.pop(0)

        - anand

        Comment

        • Steven Bethard

          #5
          Re: multiple assignment

          Anand wrote:[color=blue]
          > Wouldn't it be nice to say
          >
          > id, *tokens = line.split(',')[/color]

          id, tokens_str = line.split(',', 1)

          STeVe

          Comment

          • Anand

            #6
            Re: multiple assignment

            >> Wouldn't it be nice to say[color=blue][color=green]
            >> id, *tokens = line.split(',')[/color]
            >
            >
            > id, tokens_str = line.split(',', 1)[/color]

            But then you have to split tokens_str again.

            id, tokens_str = line.split(',', 1)
            tokens = tokens_str.spli t(',')

            this is too verbose.

            anand

            Comment

            • Steven Bethard

              #7
              Re: multiple assignment

              Anand wrote:[color=blue][color=green][color=darkred]
              >>> Wouldn't it be nice to say
              >>> id, *tokens = line.split(',')[/color]
              >>
              >> id, tokens_str = line.split(',', 1)[/color]
              >
              > But then you have to split tokens_str again.
              >
              > id, tokens_str = line.split(',', 1)
              > tokens = tokens_str.spli t(',')[/color]

              Sorry, it wasn't clear that you needed the tokens from the original post.

              If you're interested in this, the best route is to write up a PEP and
              provide an implementation. The new AST in Python makes this kind of
              thing a bit easier.

              Steve

              Comment

              • Magnus Lycka

                #8
                Re: multiple assignment

                Anand wrote:[color=blue]
                > Suppose i have a big list and i want to take tke the first one and rest
                > of the list like car/cdr in lisp.
                > is there any easy way to do this in python?[/color]

                It seems like overkill to me to make the syntax more
                complex just to avoid writing a one-line function.

                def first_rest(seq) : return seq[0], seq[1:]

                Comment

                • bruno at modulix

                  #9
                  Re: multiple assignment

                  Anand wrote:[color=blue][color=green][color=darkred]
                  >>>Wouldn't it be nice to say
                  >>>id, *tokens = line.split(',')[/color]
                  >>
                  >>
                  >>id, tokens_str = line.split(',', 1)[/color]
                  >
                  >
                  > But then you have to split tokens_str again.
                  >
                  > id, tokens_str = line.split(',', 1)
                  > tokens = tokens_str.spli t(',')
                  >
                  > this is too verbose.
                  >[/color]

                  head_tail = lambda seq: seq[0], seq[1:]

                  ....
                  id, tokens = head_tail(line. split(','))


                  but I do like the syntax you're proposing !-)

                  --
                  bruno desthuilliers
                  python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
                  p in 'onurb@xiludom. gro'.split('@')])"

                  Comment

                  Working...