Implicit lists

Chirayu Krishnappa thephoenix235 at gmx.net
Thu Jan 30 16:13:28 EST 2003


Hi,

You've definitely received a lot of responses on how to make it a
general purpose function. 

I'd like to ask how you get these maybe arg or list things in the
first place. i presume they are the result of a function call. do you
have too many such functions? (generally well written function which
might return a list will return a list even for a single element so
you can have uniformly written code. the cgi module's FieldStorage
class' getitem() function is a notable exception.) 

if you find that you need to do this for just a small bunch of
functions - perhaps you could write wrappers for those functions to
always return a list and use them instead.

Chirayu.

On Thu, 30 Jan 2003 14:41:33 +0000, Dale Strickland-Clark
<dale at riverhall.NOTHANKS.co.uk> wrote:

>In many places, often small utility functions, I find myself using the
>form:
>
>lst = maybeAnArg
>if type(lst) not in (list, tuple)
>    lst = [lst]
>for x in lst:
>    whatever
>
>or
>
>lst = maybeAnArg
>if type(lst) not in (list, tuple)
>    lst = [lst]
>mangle = [x for x in lst if whatever]
>
>
>In other words, I want to be able to treat an argument of arbitrary
>type as a list of 1 if it isn't already a list.
>
>Has anyone got a neater way of doing this?
>
>Thanks





More information about the Python-list mailing list