string: __iter__()?

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

    #1

    string: __iter__()?

    Hello!

    Just for curiosity i'd like to know why strings don't support the
    iteration protocoll!? Is there some deeper reason for this?
    >>hasattr('Some String', '__iter__')
    False

    In Python 2.5 it's actually simple to obtain one:
    >>myIter = (c for c in 'SomeString')
    >>myIter.next ()
    'S'

    Thanks for info!

    Chris
  • Fredrik Lundh

    #2
    Re: string: __iter__()?

    mrquantum wrote:
    Just for curiosity i'd like to know why strings don't support the
    iteration protocoll!?
    really? iter("SomeStrin g") works just fine for me.

    </F>




    Comment

    • Paul Rubin

      #3
      Re: string: __iter__()?

      "mrquantum" <mrquantum@holo n.atwrites:
      Just for curiosity i'd like to know why strings don't support the
      iteration protocoll!? Is there some deeper reason for this?
      >
      >hasattr('SomeS tring', '__iter__')
      False
      It is a little but odd. But at least in 2.3.4:

      Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
      [GCC 3.4.2 20041017 (Red Hat 3.4.2-6.fc3)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>a = iter('hi there')
      >>a
      <iterator object at 0xb7c7f8ec>
      >>print [x for x in a]
      ['h', 'i', ' ', 't', 'h', 'e', 'r', 'e']
      >>>
      So it looks like the iter is still there, but comes from some older
      layer of the implementation.

      Comment

      • mrquantum

        #4
        Re: string: __iter__()?

        Am Wed, 04 Oct 2006 12:03:41 +0200 schrieb Fredrik Lundh:
        >
        really? iter("SomeStrin g") works just fine for me.
        >
        Hmm, right!

        But then why doesn't dir('SomeString ') show an __iter__ method? Seems to
        be implmented differently than with e.g. lists? Know why?

        Comment

        • Peter Otten

          #5
          Re: string: __iter__()?

          mrquantum wrote:
          Am Wed, 04 Oct 2006 12:03:41 +0200 schrieb Fredrik Lundh:
          >really? iter("SomeStrin g") works just fine for me.
          But then why doesn't dir('SomeString ') show an __iter__ method? Seems to
          be implmented differently than with e.g. lists?
          The older pre-__iter__() iteration style relying on __getitem__() still
          works:
          >>class A:
          .... def __getitem__(sel f, index):
          .... return [3,2,1][index]
          ....
          >>for item in A():
          .... print item
          ....
          3
          2
          1
          Know why?
          No idea. Perhaps nobody cared?

          Peter

          Comment

          • mrquantum

            #6
            Re: string: __iter__()?

            Am Wed, 04 Oct 2006 11:59:07 +0200 schrieb mrquantum:
            Hello!
            >
            Just for curiosity i'd like to know why strings don't support the
            iteration protocoll!? Is there some deeper reason for this?
            >
            Sorry, was to hasty by saying "... don't support the iteration
            protocol'! Sure they do, as the iter('SomeStrin g') function or the
            construction for c in 'SomeString' show.

            It's just not implemented by a __iter__ method of the string in question!

            Comment

            • mrquantum

              #7
              Re: string: __iter__()?

              Am Wed, 04 Oct 2006 12:24:48 +0200 schrieb Peter Otten:
              >
              The older pre-__iter__() iteration style relying on __getitem__() still
              works:
              >
              >>>class A:
              ... def __getitem__(sel f, index):
              ... return [3,2,1][index]
              ...
              >>>for item in A():
              ... print item
              ...
              3
              2
              1
              >
              Thanks! I see:
              >>class B:
              .... def __iter__(self):
              .... for i in xrange(3):
              .... yield [3,2,1][i]
              ....
              >>myIter = B().__iter__()
              >>myIter.next ()
              3
              >>myIter.next ()
              2
              >>myIter.next ()
              1

              Chris

              Comment

              • Michele Simionato

                #8
                Re: string: __iter__()?

                mrquantum wrote:
                Hello!
                >
                Just for curiosity i'd like to know why strings don't support the
                iteration protocoll!? Is there some deeper reason for this?
                >
                >hasattr('SomeS tring', '__iter__')
                False
                >
                In Python 2.5 it's actually simple to obtain one:
                >
                >myIter = (c for c in 'SomeString')
                >myIter.next( )
                'S'
                >
                Thanks for info!
                >
                Chris
                Well, I see it as a feature. Typically I want to consider a string as
                an atomic object (and
                not as a sequence of characters) and I can check hasattr(obj,
                '__iter__') to distinguish
                (for instance) a list of strings from a single string (typically in
                recursive algorithms
                working on texts).

                Michele Simionato

                Comment

                • John Roth

                  #9
                  Re: string: __iter__()?


                  mrquantum wrote:
                  Hello!
                  >
                  Just for curiosity i'd like to know why strings don't support the
                  iteration protocoll!? Is there some deeper reason for this?
                  >
                  >hasattr('SomeS tring', '__iter__')
                  False
                  >
                  In Python 2.5 it's actually simple to obtain one:
                  >
                  >myIter = (c for c in 'SomeString')
                  >myIter.next( )
                  'S'
                  >
                  Thanks for info!
                  >
                  Chris
                  The iter() builtin creates an iterator for any
                  object that obeys the sequence protocol.
                  Since strings obey the sequence protocol there
                  is no real advantage to adding yet another
                  protocol to an already very fat object.

                  This does, however, mean that testing
                  for the presence of __iter__ is incomplete;
                  one also has to test for __getattr__ if the
                  object doesn't have an __Iter__ method.

                  Depending on your program logic, it
                  may be easier to just use iter() and
                  handle the exception if it fails.

                  See PEP 234 for a discussion of the
                  reasons for doing it this way.

                  John Roth

                  Comment

                  • mrquantum

                    #10
                    Re: string: __iter__()?

                    John Roth:
                    The iter() builtin creates an iterator for any
                    object that obeys the sequence protocol.
                    Since strings obey the sequence protocol there
                    is no real advantage to adding yet another
                    protocol to an already very fat object.
                    Okay!
                    This does, however, mean that testing
                    for the presence of __iter__ is incomplete;
                    one also has to test for __getattr__ if the
                    object doesn't have an __Iter__ method.
                    Should be __getitem__ and not __getattr__!?
                    Depending on your program logic, it
                    may be easier to just use iter() and
                    handle the exception if it fails.
                    Okay, I'll do it this way - except will then raise a TypeError, as I just
                    found in the docs!
                    See PEP 234 for a discussion of the
                    reasons for doing it this way.
                    >
                    Thanks for pointing this out!

                    Chris

                    PS: Thanks to all posters in this thread for your illuminative comments!

                    Comment

                    • Terry Reedy

                      #11
                      Re: string: __iter__()?


                      "mrquantum" <mrquantum@holo n.atwrote in message
                      news:68c55$4523 8d46$506d0b9f$1 2642@news.chell o.at...
                      Sorry, was to hasty by saying "... don't support the iteration
                      protocol'! Sure they do, as the iter('SomeStrin g') function or the
                      construction for c in 'SomeString' show.
                      >
                      It's just not implemented by a __iter__ method of the string in question!
                      I am sure this will change in 3.0 and possibly 2.x.
                      I believe the intention is that 'is iterable' will become the same as 'has
                      __iter__()'.

                      tjr



                      Comment

                      Working...