why cannot assign to function call

Joe Strout joe at strout.net
Thu Jan 8 00:34:14 CET 2009

Dan Esch wrote:

> In essence, the implication of immutability for Python is that there is 
> only one "parrot", one "spam,"in fact one anything. (This seems like it 
> must hold for data primitives - does it hold for complex objects as 
> well? It seems it must...) In addition there is only one 1, and one 2 
> etc.

That's not necessarily true.  If you have

   a = "par" + "rot"
   b = "parrot"

then, most likely (though it depends on how clever the compiler 
optimizations are), there are two different string objects containing 
the data "parrot".  In this case, "a == b" is true because string 
equality testing compares the data, but "a is b" is false because a and 
b refer to different objects.

And by the way, don't let the people claiming that Python's assignment 
model is bizarre confuse you.  Assignment and parameter-passing in 
Python is exactly the same as in any other modern OOP language; it just 
so happens that in Python, all variables are references, while in most 
other languages, some variables are references and others are primitive 
types.  But it should be obvious (though apparently isn't, to some) that 
this restricted, uniform data model does not fundamentally change 
anything; it just makes Python a little more restricted and uniform. 
For details, see: <http://www.strout.net/info/coding/valref/>

- Joe

More information about the Python-list mailing list