[Tutor] flatten a tuple

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 18 Apr 2001 20:35:35 +0200


On  0, Sean 'Shaleh' Perry <shaleh@valinux.com> wrote:
> BTW, list.extend() requires a list too, so it should have been
> result.extend(list(tup)).

It can take a sequence, I tested that.

>>> x=[]
>>> x.extend((1,2,3))
>>> x
[1, 2, 3]

Hmm. Grumble. Writing *this* post, I dug into the actual docs.
http://www.python.org/doc/current/lib/typesseq-mutable.html

s.extend(x)      Same as s[len(s):len(s)] = x  (2)

(2) Raises an exception when x is not a list object. The extend() method is
    experimental and not supported by mutable sequence types other than lists.
    
Well, it does not raise an exception when used with a tuple! But since the
lib ref says it's illegal, it may disappear at any point :-(.

I *hate* things that work although it is by accident. When you're coding
Python and suspect that something works, you're going to test it in the
interpreter. You're not going to dig into the references :-(.

And s[len(s):len(s)] = x does give the exception. Ah well. Just another
reason to use lists in the first place :-).

-- 
Remco Gerlich