why cannot assign to function call

Mark Wooding mdw at distorted.org.uk
Tue Jan 6 15:32:04 CET 2009

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, or a /reference/ to the list?  If you
answer the first, you'll want Tcl/C/Fortran semantics.  If you answer
the second, you'll want Lisp/Python/Javascript semantics.  If you answer
`it depends', you'll want to be confused.

Python decided that all values are passed around as and manipulated
through references.  (There are no locatives: references are not values,
and you can't have a reference to a reference.)  Lists store references;
tuples store references; and so on.

If one is to be able to implement complex data structures which involve
sharing of data (e.g., cyclic graphs) or which allow efficient
reorganization (e.g., various kinds of trees) we need to be able to
manage references to values somehow.  There are a number of ways of
doing this.

  * Everything is a reference.  This is the most uniform.  Once you've
    got the hang of it, there are no surprises.

  * Nothing is a reference.  If you want complex data structures, you're
    either screwed (e.g., if your data model is too weak) or you have to
    invent them yourself using array indices or something wretched like
    that.  Tcl is in this category: you have to mess with (associative)
    arrays or variable names to make complex data structures.

  * Some values are references to others.  This is like C, and causes
    exciting problems when you get confused.  C++ makes things more
    confusing because dereferencing can happen implicitly.  Perl manages
    to be in this camp /and/ the first one.  It's very strange.

  * Some things are references, and some aren't.  This is what Java and
    C# do.  Java says that only simple, atomic things are
    non-references, so assignment looks the same either way, but you
    can't store primitive things in containers expecting to store
    references (until Sun added automatic boxing and unboxing --
    eventually).  C# makes matters more complicated by letting users
    define structured mutable data which aren't handled by reference,
    but does the boxing and unboxing thing transparently.

> As for there being no assignment in algebra, is that not really what
> variable substitution is? 


> They have different names, but in my estimation they perform exactly
> the same function.  You're assigning specific values to the variables
> in an expression so that you can solve for a constant... just like in
> a computer program.

When you do this, you construct a different expression.  They might have
equal values, but they're still different.  (You can consider
equivalence classes under some appropriate equivalence relation if you
like, but that just makes things more complex.)

> There is even an allocation of memory: it's in your brain. :D

Ahh!  You're modifying a memory location in your brain so that it refers
to a different expression.  Isn't this the Lisp/Python/Javascript model?

-- [mdw]

More information about the Python-list mailing list