detecting variable types

Andrew Koenig ark at acm.org
Wed Sep 22 16:46:38 EDT 2004


"Jay" <wjjeonk at hotmail.com> wrote in message 
news:ciskpq$7f2$1 at news-int.gatech.edu...

> Here's what I'm trying to do:
>
> I have a function like this:
>
> def func(**params):
>
>    # if params[key1] is a single string
>        # do something with params[key1]
>
>    # if params[key1] is a list of strings
>        for val in params[key1]:
>            # do something
>
> Could you suggest a better way to do this without detecting the type?

I don't see anything particularly wrong with detecting the type this way:

    if isinstance(params[key1], list):
        for val in params[key1]:
            # do something
    else:
        # do something with params[key1]

Of course that won't work for other kinds of sequences, but if that's what 
you want, then that's what you want.





More information about the Python-list mailing list