why?

bradh at cuneata.net bradh at cuneata.net
Mon Dec 2 18:44:04 EST 2002


Quoting dsavitsk <dsavitsk at e-coli.net>:

> from
> http://www.python.org/doc/current/tut/node7.html#SECTION00730000000000000000
> 0
> 
> "Tuples, like strings, are immutable: it is not possible to assign to
> the
> individual items of a tuple (you can simulate much of the same effect
> with
> slicing and concatenation, though). It is also possible to create
> tuples
> which contain mutable objects, such as lists."
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.

> > >>> doing[1][0]
> > 'b'
This is irrelevant. doing[1] should be the same as doing[1][0]
in this particular case.

> > >>> doing[1][0] = 'i'
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in ?
> > TypeError: object doesn't support item assignment
> > >>>
You can, if you say
doing[1] = ('i')
which to my (very newbie) thinking is assignment of a tuple
to a string. I can't see how it is more valid to assign a 
tuple to a string, rather than a string to a string.

So I'm confused about this mutability business again, which 
is bad, because I thought I had it clear.

Brad




More information about the Python-list mailing list