how can i check whether a variable is iterable in my code?

Discussion in 'Python' started by satoru, Sep 20, 2008.

  1. satoru

    satoru Guest

    hi, all
    i want to check if a variable is iterable like a list, how can i
    implement this?
     
    satoru, Sep 20, 2008
    #1
    1. Advertising

  2. satoru

    Aidan Guest

    satoru wrote:
    > hi, all
    > i want to check if a variable is iterable like a list, how can i
    > implement this?


    this would be one way, though I'm sure others exist:

    if hasattr(yourVar, '__iter__'):
    # do stuff
     
    Aidan, Sep 20, 2008
    #2
    1. Advertising

  3. satoru

    satoru Guest

    On Sep 20, 6:35 pm, Aidan <> wrote:
    > satoru wrote:
    > > hi, all
    > > i want to check if a variable is iterable like a list, how can i
    > > implement this?

    >
    > this would be one way, though I'm sure others exist:
    >
    > if hasattr(yourVar, '__iter__'):
    >         # do stuff


    thank you,but this will miss out sequences like string just because it
    doesn't have an attribute named '__iter__'
     
    satoru, Sep 20, 2008
    #3
  4. satoru wrote:

    > hi, all
    > i want to check if a variable is iterable like a list, how can i
    > implement this?


    untested

    def is_iterable(param):
    try:
    iter(param)
    except TypeError:
    return False
    else:
    return True

    --
    By ZeD
     
    Vito De Tullio, Sep 20, 2008
    #4
  5. satoru

    John Machin Guest

    On Sep 20, 8:54 pm, satoru <> wrote:
    > On Sep 20, 6:35 pm, Aidan <> wrote:
    >
    > > satoru wrote:
    > > > hi, all
    > > > i want to check if a variable is iterable like a list, how can i
    > > > implement this?

    >
    > > this would be one way, though I'm sure others exist:

    >
    > > if hasattr(yourVar, '__iter__'):
    > >         # do stuff

    >
    > thank you,but this will miss out sequences like string just because it
    > doesn't have an attribute named '__iter__'


    str objects have a __getitem__ attribute, as do other built-in
    sequence types: unicode, xrange, buffer.
    AFAIK if an object has no __iter__ but has a __getitem__, iter(obj)
    will create an iterator that calls obj.__getitem__(0),
    obj.__getitem__(1), etc until IndexError is raised.
     
    John Machin, Sep 20, 2008
    #5
  6. satoru

    satoru Guest

    thanks very much!
     
    satoru, Sep 20, 2008
    #6
  7. satoru

    Terry Reedy Guest

    satoru wrote:
    > On Sep 20, 6:35 pm, Aidan <> wrote:
    >> satoru wrote:
    >>> hi, all
    >>> i want to check if a variable is iterable like a list, how can i
    >>> implement this?

    >> this would be one way, though I'm sure others exist:
    >>
    >> if hasattr(yourVar, '__iter__'):
    >> # do stuff

    >
    > thank you,but this will miss out sequences like string just because it
    > doesn't have an attribute named '__iter__'


    In 3.0, it does. Such consistency is one of the advantages of 3.0.

    In at least some 2.x's, str still uses the older __getitem__ iteration
    protocol. I am not sure about other built-in sequences.
     
    Terry Reedy, Sep 20, 2008
    #7
  8. satoru

    James Mills Guest

    satoru,

    I should point out that the normal
    approach is to just try whatever it
    is that you're doing, and let it fail
    where it fails. For example:

    def processSeq(x):
    for i in x:
    print i

    processSeq([1, 2, 3])
    processSeq("foobar")
    processSeq(5) <-- This will fail.

    cheers
    James

    On Sat, Sep 20, 2008 at 7:28 PM, satoru <> wrote:
    > hi, all
    > i want to check if a variable is iterable like a list, how can i
    > implement this?
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    --
    -- "Problems are solved by method"
     
    James Mills, Sep 21, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Curt_C [MVP]
    Replies:
    1
    Views:
    534
  2. Replies:
    2
    Views:
    372
    James Stroud
    Dec 10, 2008
  3. JB
    Replies:
    1
    Views:
    559
  4. Laurent
    Replies:
    20
    Views:
    485
    Chris Angelico
    Sep 9, 2011
  5. tony
    Replies:
    14
    Views:
    266
    A. Sinan Unur
    Mar 12, 2006
Loading...

Share This Page