Modifying Class Object

Alf P. Steinbach alfps at start.no
Tue Feb 16 00:45:34 CET 2010


* Steven D'Aprano:
> On Mon, 15 Feb 2010 21:25:23 +0000, Arnaud Delobelle wrote:
> 
>> John Posner <jjposner at optimum.net> writes: [...]
>>>>   x = s[0]
>> [...]
>>>   assigns the name *x* to the object that *s[0]* refers to
>> s[0] does not refer to an object, it *is* an object (once evaluated of
>> course, otherwise it's just a Python expression).
> 
> Precisely. Treated as an expression, that is, as a string being evaluated 
> by the compiler, we would say that it *refers to* an object (unless 
> evaluation fails, in which case it refers to nothing at all). But treated 
> as whatever you get after the compiler is done with it, that is, post-
> evaluation, we would say that it *is* an object.
> 
> This subtle distinction is essentially the difference between a label and 
> the thing that is labeled.

The main differences between a pure functional language where that view can hold 
and be reasonable, and a language like Python, are that

   * Python assignments change which object a name refers to, /at runtime/.

     - Name binding is run time action, not a compile time action.
       Without run time binding names couldn't be reassigned. s = 1; s = 2

   * Some Python objects are modifiable (a.k.a. mutable).

     - This is particularly important when two or more names refer to the
       same object and that object is modified.

That is, a simple-minded transfer of concepts from pure functional programming 
to Python breaks down in these cases[1].

I hope this explanation of exactly where the functional programming enthusiasts 
here go wrong can be of help to readers of the thread, although I've given up 
hope on those holding the functional programming view (since I'm only human, and 
even the Gods contend in vain against that sort of thing).


Cheers & hth.,

- Alf

Notes:
[1] Steven D'Aprano's focus on compilation rather than execution mainly ignores 
the first point, that a name in given a statement in a loop, say, can refer to 
different objects in different loop iterations. Happily the Python language 
specification explains binding as a run-time action. Binding is not a compile 
time action in Python, it is a run-time action, and can bind a given name in a 
given statement within the same routine execution, to different objects, and the 
language specification of course uses the phrase "refers to" to explain the 
situation after a run time binding.



More information about the Python-list mailing list