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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • satoru

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

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

    #2
    Re: how can i check whether a variable is iterable in my code?

    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

    Comment

    • satoru

      #3
      Re: how can i check whether a variable is iterable in my code?

      On Sep 20, 6:35 pm, Aidan <ai...@gmail.co mwrote:
      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__'

      Comment

      • Vito De Tullio

        #4
        Re: how can i check whether a variable is iterable in my code?

        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(par am):
        try:
        iter(param)
        except TypeError:
        return False
        else:
        return True

        --
        By ZeD

        Comment

        • John Machin

          #5
          Re: how can i check whether a variable is iterable in my code?

          On Sep 20, 8:54 pm, satoru <torainLi...@gm ail.comwrote:
          On Sep 20, 6:35 pm, Aidan <ai...@gmail.co mwrote:
          >
          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.

          Comment

          • satoru

            #6
            Re: how can i check whether a variable is iterable in my code?

            thanks very much!

            Comment

            • Terry Reedy

              #7
              Re: how can i check whether a variable is iterable in my code?

              satoru wrote:
              On Sep 20, 6:35 pm, Aidan <ai...@gmail.co mwrote:
              >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.

              Comment

              • James Mills

                #8
                Re: how can i check whether a variable is iterable in my code?

                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("foo bar")
                processSeq(5) <-- This will fail.

                cheers
                James

                On Sat, Sep 20, 2008 at 7:28 PM, satoru <torainLight@gm ail.comwrote:
                hi, all
                i want to check if a variable is iterable like a list, how can i
                implement this?
                --

                >


                --
                --
                -- "Problems are solved by method"

                Comment

                Working...