Implicit lists

Dale Strickland-Clark dale at riverhall.NOTHANKS.co.uk
Thu Jan 30 10:39:13 EST 2003


Alex Martelli <aleax at aleax.it> wrote:


>How aslist is written is obviously quite secondary (at least,
>I _hope_ that's obvious...) -- all of its USES will be just as
>neat as the above example.
>
>If it turns out (as would seem to be the case) that what you
>need is actually an iterator (since you want to treat tuples
>as "lists", you ain't gonna be able to append, sort, &c
>anyway) then I'd name the auxiliary function "iteron" or
>something like that.  In my experience, what I generally
>want is:
>    treat strings, and all string-like objects, as non-sequences
>    otherwise, iterate directly on sequences and other iterables
>"other iterables" is debatable (files, dictionaries, ...) but
>that's how I've generally found things work best -- easy enough
>to tweak it to your liking otherwise.  So, for example:
>
>def iteron(something):
>    # string-like objects: treat as non-sequences
>    try: something+''
>    except TypeError: pass
>    else:
>        yield something
>        return
>    # other sequences
>    try:
>        for x in something: yield x
>    except TypeError:
>        yield something
>
>
>Alex

Thanks for that. I was hoping for something that didn't require an
extra module but I guess a single general purpose function wouldn't be
so bad.

--
Dale Strickland-Clark
Riverhall Systems Ltd




More information about the Python-list mailing list