why cannot assign to function call

Joe Strout joe at strout.net
Thu Jan 8 23:22:17 EST 2009


rurpy at yahoo.com wrote:

> "the same as anyone else's" only if [Python's] "idea
> of assignment" does not include producing the same
> results.
> 
>   a = array (1,2,3)
>   b = a
>   a[1] = 4
>   print b
> 
> C, C++, VBA, Fortran, Perl:  1, 2, 3
> Python:  1, 4, 3

You are mistaken (except perhaps in the Fortran case, which is an 
oddball by modern standards, and I don't know Perl well enough to judge).

C/C++ code:

  int* a = malloc(3);
  a[0] = 1;
  a[1] = 2;
  a[2] = 3;
  int* b = a;
  a[1] = 4;
  print_array(b)
  ---> Result: 1, 4, 3

REALbasic code:

  Dim a() As Integer = Array(1,2,3)
  Dim b() As Integer = a
  a(1) = 4
  PrintArray b
  --> Result: 1, 4, 3

VB.NET code would be very similar in syntax, and identical in behavior, 
to the REALbasic code.  Java would also have the same semantics, though 
my Java is too rusty to get the syntax right without actually trying it.

If you can find a language where an array variable is NOT a reference 
(and thus does not behave exactly as in Python, Java, REALbasic, C++, 
etc.), then that language is either a dinosaur or some weird academic 
oddity.

Best,
- Joe





More information about the Python-list mailing list