why cannot assign to function call
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Tue Jan 6 20:02:54 EST 2009
On Tue, 06 Jan 2009 14:32:04 +0000, Mark Wooding wrote:
> Derek Martin <code at pizzashack.org> wrote:
>
>> I think I have though, not that it matters, since that was never really
>> my point. Python's assignment model requires more explanation than the
>> traditional one, simply to state what it does. That alone is evidence
>> (but not proof).
>
> Hmm. Actually, it's not the assignment model which is strange at all.
> It's the data model. What does an expression like
>
> [1, 2, 3]
>
> denote? Is it the list itself,
Yes.
> or a /reference/ to the list?
No.
> If you answer the first, you'll want Tcl/C/Fortran semantics.
Absolutely not! I want *Python* semantics, and amazingly enough, that's
*just what Python gives*.
> If you answer the second, you'll want Lisp/Python/Javascript
> semantics.
If I wanted a reference to a list, I'd expect to *dereference* the
reference to get to the list. That's not what Python forces you do to:
you just use the list as the list object itself.
This isn't hard people. Stop confusing the implementation details of how
CPython works under the hood with Python level code.
In Python, [1, 2, 3] is a list, not a reference to a list. When you pass
[1, 2, 3] to a function, the function sees the list you passed it. The
function doesn't see "a reference to a list", it sees a list:
>>> def func(x):
... print type(x)
...
>>> func([1, 2, 3])
<type 'list'>
It's so easy, some people refuse to believe it could be that easy, and
insist on complicating matters by bring the implementation details into
the discussion. Just stop, please. References belong in the
*implementation*, nothing to do with Python level code.
In Python code, there are no references and no dereferencing.
> Python decided that all values are passed around as and manipulated
> through references.
Oh really? Then why can't I write a Python function that does this?
x = 1
y = 2
swap(x, y)
assert x == 2
assert y == 1
You can't, because Python doesn't have references. In a language with
references, that's easy. Here's an untested Pascal version for swap:
procedure swap(var x: integer, var y: integer);
var
tmp: integer;
begin
tmp := x;
x := y;
y := x;
end;
It's harder (impossible?) to write a version that will operate on
arbitrary types, but that's statically typed languages for you.
> (There are no locatives: references are not values,
But references *are* locatives.
> and you can't have a reference to a reference.) Lists store references;
> tuples store references; and so on.
No no no, lists and tuples store *objects*. Such storage happens to be
implemented as pointers in CPython, but that's an irrelevant detail at
the level of Python.
--
Steven
More information about the Python-list
mailing list