why?
Erik Max Francis
max at alcyone.com
Mon Dec 2 20:43:33 EST 2002
bradh at cuneata.net wrote:
> Quoting dsavitsk <dsavitsk at e-coli.net>:
> > "Tuples, like strings, are immutable: ...
>
> That is what I thought too. But it doesn't match the observed
> behaviour.
>
> > > >>> doing = ['a','b','c']
> > > >>> doing[1] = ('b','c')
>
> doing[1] is a string, and it shouldn't be possible to
> assign to an immutable object.
If L is a sequence, then L[i] = x is modifying the _sequence_, not the
element of the sequence:
>>> L = [1, 2, 3]
>>> L[1] = 'a'
>>> L
[1, 'a', 3]
> > > >>> doing[1][0]
> > > 'b'
>
> This is irrelevant. doing[1] should be the same as doing[1][0]
> in this particular case.
No, because at this point doing has a tuple as the second argument. One
of the beauties of Python's interactive interpreter is that it makes it
easy to test things out. Try it yourself:
>>> doing = ['a', 'b', 'c']
>>> doing[1] = ('b', 'c')
>>> doing
['a', ('b', 'c'), 'c']
> > > >>> doing[1][0] = 'i'
> > > Traceback (most recent call last):
> > > File "<stdin>", line 1, in ?
> > > TypeError: object doesn't support item assignment
So the error is is that you are trying to replace the first element of
the _tuple_ with 'i'. The problem isn't with strings, it's with tuples;
tuples are immutable and so you cannot modify them.
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ Heaven and Hell / Is on Earth
\__/ Salt-n-Pepa
Bosskey.net: Aliens vs. Predator 2 / http://www.bosskey.net/avp2/
A personal guide to Aliens vs. Predator 2.
More information about the Python-list
mailing list