why cannot assign to function call

rurpy at yahoo.com rurpy at yahoo.com
Wed Jan 7 19:45:06 EST 2009


On Jan 5, 12:21 pm, Derek Martin <c... at pizzashack.org> wrote:
...
> I understand why the assignment model works the way it does, and it's
> quite sensible, *when you understand it*.  However, I do also think
> that to someone who has not encountered such a model before, and who
> has not had it explained to them, and/or who has not the background to
> understand why it is implemented that way, it very likely might seem
> "markedly unusual in appearance, style, or general character and often
> involving incongruous or unexpected elements;" as dictionary.com
> defines the term bizarre.  So no, I don't think that's a
> mischaracterization at all.
...

Ignoring the nit-picking over choice of words, or what
languages are taught in CS courses, I agree with Derek's
general point that Python's assignment semantics is *not*
the same as many other common languages, and that this
confuses people coming to Python from those languages,
as evidenced by frequent postings to this list by those
people.  I concluded exactly the same a long time ago.

When I started using Python I had no problem with Python's
assignment semantics because I had been using references in
Perl for years.  I did not have a very hard time with Perl's
references, after I recognized their similarities to C's
pointers.  But as I recall, it took a *long* time to wrap
my mind around C pointers.

Here is the output of set of equivalent programs in five
common (at least at one time) languages (code below) that I
have worked in and that formed my "intuition" about how
assignment works.

Sure looks to me as though Python is the odd man out.

Perl:
1, 4, 3
1, 2, 3

Basic (MS VBA):
 1             4             3
 1             2             3

C:
1, 4, 3
1, 2, 3

Fortran:
           1           4           3
           1           2           3

Python:
[1, 4, 3]
[1, 4, 3]

Of course, one can dismiss these as non-mainstream or
non-modern languages, and maintain that all "important"
languages have the same assignment semantics as Python,
but then one is left having to explain the frequent
confusion about assignment that is visible on this list.
I find Derek's explantion more plausible than any others
I have read in this thread.

========================================================
Python:
#/usr/bin/python
a = [1,2,3]
b = a
a[1] = 4
print a
print b

========================================================
Perl:
#!/usr/bin/perl
@a = (1,2,3);
@b = @a;
$a[1] = 4;
print join (',', @a), "\n";
print join (',', @b), "\n";

========================================================
Basic:
Sub test()
        Dim a, b
        a = Array(1, 2, 3)
        b = a
        a(1) = 4
        Debug.Print a(0), a(1), a(2)
        Debug.Print b(0), b(1), b(2)
        End Sub

========================================================
C:
#include <stdio.h>
main () {
        int i; struct {int a[3];} a, b;
        for (i=0; i<3; i++) {
            a.a[i] = i+1; }
        b = a;
        a.a[1] = 4;
        printf ("%i,%i,%i\n", a.a[0], a.a[1], a.a[2]);
        printf ("%i,%i,%i\n", b.a[0], b.a[1], b.a[2]);
        }

==========================================================
Fortran:
      program test
      integer a(3), b(3)
      do 100, i=1,3
          a(i) = i
100       continue
      b = a
      a(2) = 4
      write(unit=*,fmt=*) a
      write(unit=*,fmt=*) b
      end



More information about the Python-list mailing list