newbie idiom question

Jeff Epler jepler at inetnebr.com
Tue Jun 22 14:21:00 EDT 1999


To add to the confusion, consider

>>> eggs = [[1], [2], [3]]
>>> for spam in eggs:
...      spam[:]=['cooked'] # or spam[0] = 'cooked'
...
... eggs
[['cooked'], ['cooked'], ['cooked']]

Here, the item "spam" is a mutable object, and I modify it.

In your case, 
>      >>> for spam in eggs:
>      ...	spam = 'cooked'

spam begins the loop as a reference to something from eggs, but all you do
is make spam a reference to something else. (which doesn't change the
references in eggs)

Remember, in Python the "=" operator means "put a reference to the RHS in
the LHS", and has no effect on the object that the LHS used to be a
reference to (if it had a prior value).  Well, except in the case of
setitem/setslice, of course. (which is what I used in the above example)

Is this a wart on python, that "=" means three different things depending
if you have a name, a [item] or a [slice:] on the LHS?

Jeff




More information about the Python-list mailing list