How to test if object is sequence, or iterable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim N. van der Leeuw

    How to test if object is sequence, or iterable?

    Hi,

    I'd like to know if there's a way to check if an object is a sequence,
    or an iterable. Something like issequence() or isiterable().

    Does something like that exist? (Something which, in case of iterable,
    doesn't consume the first element of the iterable)

    Regards,

    --Tim

  • Bruno Desthuilliers

    #2
    Re: How to test if object is sequence, or iterable?

    Tim N. van der Leeuw a écrit :
    Hi,
    >
    I'd like to know if there's a way to check if an object is a sequence,
    or an iterable. Something like issequence() or isiterable().
    >
    Does something like that exist? (Something which, in case of iterable,
    doesn't consume the first element of the iterable)
    isiterable = lambda obj: isinstance(obj, basestring) \
    or getattr(obj, '__iter__', False)


    Should cover most cases.

    Comment

    • Tim N. van der Leeuw

      #3
      Re: How to test if object is sequence, or iterable?


      Bruno Desthuilliers wrote:
      Tim N. van der Leeuw a écrit :
      Hi,

      I'd like to know if there's a way to check if an object is a sequence,
      or an iterable. Something like issequence() or isiterable().

      Does something like that exist? (Something which, in case of iterable,
      doesn't consume the first element of the iterable)
      >
      isiterable = lambda obj: isinstance(obj, basestring) \
      or getattr(obj, '__iter__', False)
      >
      >
      Should cover most cases.
      Yes, that seems to cover all cases I can think of, indeed. Funny
      though, that string objects do not have an '__iter__' method, but are
      still iterable... But it will make most of my use-cases easier: Often I
      want to iterate over something, if it's an iterable, except when it's a
      string.


      Thanks,

      --Tim

      Comment

      • Terry Reedy

        #4
        Re: How to test if object is sequence, or iterable?


        "Tim N. van der Leeuw" <tim.leeuwvande r@nl.unisys.com wrote in message
        news:1153587482 .812348.320630@ h48g2000cwc.goo glegroups.com.. .
        Hi,
        >
        I'd like to know if there's a way to check if an object is a sequence,
        or an iterable. Something like issequence() or isiterable().
        How about
        try: it = iter(possible_i terable)
        except TypeError: bail()

        Terry Jan Reedy



        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: How to test if object is sequence, or iterable?

          In <44c26ec2$0$216 07$636a55ce@new s.free.fr>, Bruno Desthuilliers wrote:
          Tim N. van der Leeuw a écrit :
          >Hi,
          >>
          >I'd like to know if there's a way to check if an object is a sequence,
          >or an iterable. Something like issequence() or isiterable().
          >>
          >Does something like that exist? (Something which, in case of iterable,
          >doesn't consume the first element of the iterable)
          >
          isiterable = lambda obj: isinstance(obj, basestring) \
          or getattr(obj, '__iter__', False)
          >
          >
          Should cover most cases.
          What about objects that just implement an apropriate `__getitem__()`
          method?

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • Bruno Desthuilliers

            #6
            Re: How to test if object is sequence, or iterable?

            Marc 'BlackJack' Rintsch a écrit :
            In <44c26ec2$0$216 07$636a55ce@new s.free.fr>, Bruno Desthuilliers wrote:
            >
            >
            >>Tim N. van der Leeuw a écrit :
            >>
            >>>Hi,
            >>>
            >>>I'd like to know if there's a way to check if an object is a sequence,
            >>>or an iterable. Something like issequence() or isiterable().
            >>>
            >>>Does something like that exist? (Something which, in case of iterable,
            >>>doesn't consume the first element of the iterable)
            >>
            >>isiterable = lambda obj: isinstance(obj, basestring) \
            > or getattr(obj, '__iter__', False)
            >>
            >>
            >>Should cover most cases.
            >
            >
            What about objects that just implement an apropriate `__getitem__()`
            method?
            Hmmm... (quick test)

            Good point.

            FWIW, Terry's solution might be far better.

            Comment

            • Nick Vatamaniuc

              #7
              Re: How to test if object is sequence, or iterable?

              Tim,

              An object is iterable if it implements the iterator protocol. A good
              enough check to see if it does is to check for the presense of the
              __iter__() method. The way to do it is:
              hasattr(object, '__iter__')

              You are correct in the fact that you check if an object is iterable
              rather than using isinstance to check if it is of a partucular type.
              You are doing things 'the pythonic way' ;)

              Nick Vatamaniuc


              Tim N. van der Leeuw wrote:
              Hi,
              >
              I'd like to know if there's a way to check if an object is a sequence,
              or an iterable. Something like issequence() or isiterable().
              >
              Does something like that exist? (Something which, in case of iterable,
              doesn't consume the first element of the iterable)
              >
              Regards,
              >
              --Tim

              Comment

              • Terry Reedy

                #8
                Re: How to test if object is sequence, or iterable?


                "Nick Vatamaniuc" <vatamane@gmail .comwrote in message
                news:1153604445 .246639.81270@h 48g2000cwc.goog legroups.com...
                Tim,
                >
                An object is iterable if it implements the iterator protocol
                There are presently two iterator protocols. The old one will be likely be
                dropped in 3.0 (currently being discussed).
                >. A good
                enough check to see if it does is to check for the presense of the
                __iter__() method. The way to do it is:
                hasattr(object, '__iter__')
                Sorry, this check for the newer and nicer protocol but not the older one.
                >>hasattr('abc' , '__iter__')
                False

                This may change in 2.6. The defacto *version-independent* way to determine
                iterability is to call iter(ob). If it returns an iterator, you can
                iterate; if it raises TypeError, you cannot. Any it should be patched as
                necessary by the developers to continue doing the right thing in future
                versions.

                Terry Jan Reedy



                Comment

                Working...