functions, list, default parameters

Robert Kern robert.kern at
Tue Nov 2 18:24:44 CET 2010

On 11/2/10 2:12 AM, Lawrence D'Oliveiro wrote:
> In message<mailman.475.1288670833.2218.python-list at>, Robert Kern
> wrote:
>> On 2010-11-01 22:31 , Lawrence D'Oliveiro wrote:
>>> In message<8j1seqFa1eU2 at>, Gregory Ewing wrote:
>>>> Steven D'Aprano wrote:
>>>>> And how does Python know whether some arbitrary default object is
>>>>> mutable or not?
>>>> It doesn't, that's the whole point.
>>> Of course it knows. It is the one defining the concept in the first
>>> place, after all.
>> No, the Python interpreter doesn't define the concept. The Python language
>> developers did.
> The Python language developers said “let there be light”, and the Python
> interpreter gave forth light. So which one defines the concept of “light”?

I'm sorry, but that's not even a meaningful reply. They said no such thing and 
the interpreter does no such thing. Let's talk about things the developers did 
say and the things the interpreter does do, shall we?

The Python language developers defined the concept of mutable objects. However, 
they defined it in human terms, not algorithmic ones. They did not imbue the 
interpreter with a way of determining immutability.

>> "Immutable objects" are just those without an obvious API for modifying
>> them.
> They are ones with NO legal language constructs for modifying them. Hint: if
> a selector of some part of such an object were to occur on the LHS of an
> assignment, and that would raise an error, then the object is immutable. The
> interpreter already knows all this.

Incorrect. RHS method calls can often modify objects. set.add() mutates the set 
even though it does not have the usual LHS mutation methods like .__setitem__(). 
And even among the LHS APIs, raising an error does not determine immutability. 
int.__iadd__() does not raise an error, but ints are still immutable. 
list.__iadd__() does not raise an error, and lists are mutable. And even if it 
were reliable, how would the interpreter know what arguments to pass? 
list.__iadd__() doesn't work for any object. And why do you think that testing 
mutability in such a way would be safe since attempting those would necessarily 
alter the object if it is mutable?

Show me the algorithm that the interpreter can use to determine whether or not 
an object is mutable. Or better, since you think the interpreter already knows 
this, show me the implementation in the interpreter's source code.

>> With various trickeries, I can mutate any immutable object.
> None within the Python language itself. Which is what we’re talking about
> here: a language construct which is probably one of the top 3 sources of
> grief to Python newbies. And not-so-newbies.

"import ctypes" is within the Python language.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list