Suggestions for 2002

Paul Rubin phr-n2002a at nightsong.com
Sun Jan 13 06:17:39 CET 2002


Richard Jones <richardjones at optushome.com.au> writes:
> > I'd expect a,b = c,d  to work as something like:
> >
> >   temp1 = c
> >   temp2 = d
> >   a = temp1
> >   b = temp2
> 
> or, using the data structures from the original post and the example above,
> 
> >>> a = ['cat', 'dog']
> >>> i = 1
> >>> temp1 = 0
> >>> temp2 = 'boo'
> >>> i = temp1
> >>> a[i] = temp2
> >>> a
> ['boo', 'dog']
> 
> ... how is this not working correctly?

Oops, lemme try again.  The idea is that a,b=c,d is supposed to
simulate doing the assignments in parallel.  So I'd expect a,b = c,d
to work as something like (using C-style pointer notation):

  target1 = &a
  target2 = &b
  temp1 = c
  temp2 = d
  *target1 = temp1
  *target2 = temp2

Let's check if that works:

  a = ['cat', 'dog']
  i = 1
  target1 = &i
  target2 = &a[1]
  temp1 = 0
  temp2 = 'boo'
  *target1 = temp1    # set i = 0
  *target2 = temp2    # set a[1] to 'boo'

Now you have ['cat', 'boo'].

To get even more confusing, in Perl,

    @a = qw(cat dog);
    $i = 1;
    $i, $a[$i] = 0, "boo";
    print join(' ', at a),"\n";

prints "cat 0".  I'm not sure how it came up with that.



More information about the Python-list mailing list