
Facts and myths about Python names and values - stakent
http://nedbatchelder.com/text/names.html
======
nslocum
In the Wrapping Up section, there are some very interesting points that
weren't covered in the article.

    
    
      - Why is "list += seq" not the same as "list = list + seq"?
      - Why is "is" different than "==" and how come "2 + 2 is 4", but "1000 + 1 is not 1001"?
      - What's the deal with mutable default arguments to functions?
      - Why is it easy to make a list class attribute, but hard to make an int class attribute?
    

I'd love to read an article on these topics. Anyone have links?

~~~
true_religion
> 1000 + 1 is not 100

'is' actually checks memory locations and not equity.

Because of the way Python caches small integers, anything from 0 through X
will map to the same memory location. But a large integer (say 1001) will map
to a new memory location each time its created.

~~~
wahnfrieden
To get more specific, this object aliasing is an implementation detail. You
can't expect different Python implementations to do it the same.

------
topherjaynes
Ned always has fantastic articles, and the way this is lays out the mutable
values name relationship, is it just lazy programming that has multiple names
pointing to the same mutable value(s) or are their specific cases when that
would make since to have list(a) list(b) point to the same value(s)?

~~~
jerf
Because of the stuff mentioned later, that function application, for loops,
and all kinds of other things are fully "assigment" as well, Python is just
flinging around all kinds of names all over the place (it is essentially what
a Python program _is_ in many ways) and it is inevitable that there is no one-
to-one mapping, and that duplications will happen all the time. After all,

    
    
        x = 1
        y = [x, 2]
        for z in y:
    

"naturally" creates a duplicate binding on the value of x, without anything as
crass as "x = y = []". As is always the case of a three-line example in a
little post, when found in the wild this can be a much more hidden thing in
the middle of a 50-line function. (Which _of course_ nobody ever writes
because all 50-line functions are too long, and therefore, no 50-line
functions actually exist. But if they did, this sort of thing would happen in
them all the time.)

------
TazeTSchnitzel
JavaScript works identically.

------
B-Con
Personally, THIS is what an "intro to language X" article should look like. It
walks you through how the language thinks, which is one of the things you need
to learn in order to understand how to best use it and why things are the way
they are.

------
lubomir
The vizualizations on pythontutor.com are really helpful in explaining those
concepts.

