A certainl part of an if() structure never gets executed.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 16 08:50:03 EDT 2013


On Sun, 16 Jun 2013 09:22:20 +0000, Denis McMahon wrote:

>>> Python:
>>>
>>> b = 6
>>> a = b
>>>
>>> In Python, this first puts the value 6 in in a memory location and
>>> points "b" at that memory location, then makes "a" point to the same
>>> memory location as "b" points to.

That may be true in some sense for CPython, the reference implementation, 
but it is not a promise of the language. For example, in PyPy objects are 
free to move around in memory, so you cannot meaningfully speak of 
"putting 6 in a memory location" or having a variable "point to the same 
memory location".

The language promise is that the two names, "a" and "b", both refer to 
the same object. In the same way that, depending on who you ask, "Barack 
Obama", "Mr President", and "Dad" are three names referring to the same 
person. Anything more than that depends on the implementation.


[...]
> For example, in Python
> 
> a = 6
> b = a
> c = 6
> 
> a and b point to one memory location that contains the value 6 c points
> to a different memory location that contains the value 6

Well, maybe it does, maybe it doesn't. This is one area where the 
language does not specify the underlying behaviour. Because ints are 
unchangeable, immutable objects, the implementation is free to cache and 
reuse them if it wants. CPython caches small integers, but not floats. 
Other implementations may cache fewer, or more, immutable objects.

One thing which no Python implementation can do though is re-use 
*mutable* objects.


[...]
> These are really C terms, not Python terms. Stop thinking that C is
> behaving like Python.

This is certainly true!


-- 
Steven



More information about the Python-list mailing list