Implicit lists

holger krekel pyth at devel.trillke.net
Mon Feb 3 18:56:09 EST 2003


Beni Cherniavsky wrote:
> On 2003-01-31, Mel Wilson wrote:
> 
> > In article <XOq_9.48875$Hl6.6191395 at news010.worldonline.dk>,
> > Max M <maxm at mxm.dk> wrote:
> > >
> > >But frequently you will only call getWhere() with one argument. So
> > >instead of having to call it like:
> > >
> > >     def getWhere(['spam']):
> > >         "generates query"
> > >
> > >You want to call it like
> > >
> > >     def getWhere('spam'):
> > >         "generates query"
> >
> Since you only need one such "magic" argument and it's the last argument,
> let me borrow Guido's time machine::
> 
>     def getWhere(*foods):
>         return list(foods) # insert useful code here
> 
>     getWhere('spam')
>     getWhere('spam', 'eggs')
>     getWhere(*foodlist)
> 
> The point is to "refuse the temptation to guess".  In my experience,
> any [1]_ attempt to dispatch on the argument being a sequence / atom
> always *reduces* the utility of the function.

True but the OP wanted to have a pattern that allowed him
to pass objects around which can be lists or not and simply wants
to use

    for i in whatever(arg):

and have 'whatever' sort it out. If you have to know *at calling time*
what kind 'arg' is then the whole point is mute because you
can simply use

    for i in ['where']:
        ...

    for i in foodlist:
        ...

which is as short and concise as it gets. 

    holger





More information about the Python-list mailing list