why cannot assign to function call

Joe Strout joe at strout.net
Fri Jan 9 16:12:32 CET 2009


rurpy at yahoo.com wrote:

>>>   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
> 
> I don't think so.
> See http://groups.google.com/group/comp.lang.python/msg/f99d5a0d8f869b96
> The code I quoted there was tested.
> In the C/C++ case, array-to-pointer coercion confuses
> the issue so I embedded the array in a struct which
> is more like an "object".

No, that's cheating (IMHO).  Structs used directly (rather than via
pointers) are the odd beast, and while they're certainly possible in C,
they are far from the normal idiom.  And certainly embedding an array in
a struct just to force it to be copied, avoiding the standard reference
semantics, is an arbitrary trick.

I never claimed that you *couldn't* have copy semantics in C; you can do
almost anything you want in C (or C++).  But the *normal* usage of an
array is via a pointer, in which case the semantics are exactly the same
as in Python, Java, REALbasic, .NET, etc.

>(Keep in mind
> my point was not to show the behavior of arrays, but to
> show that several common languages *do not* use Python's
> "*all* names are references" model -- though of course
> this does not preclude their having some Python-like
> assignments since they all have some way of doing
> references.)

Ah.  OK then, I guess I missed you're point.  You're absolutely right;
many languages have both reference types and value types.  Python is a
bit unusual in that it has only reference types.  I would have picked a
different example to illustrate that, but it's true nonetheless.

Best,
- Joe






More information about the Python-list mailing list