473,503 Members | 7,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('SomeString', '__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
Oct 4 '06 #1
10 2281
mrquantum wrote:
Just for curiosity i'd like to know why strings don't support the
iteration protocoll!?
really? iter("SomeString") works just fine for me.

</F>


Oct 4 '06 #2
"mrquantum" <mr*******@holon.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('SomeString', '__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.
Oct 4 '06 #3
Am Wed, 04 Oct 2006 12:03:41 +0200 schrieb Fredrik Lundh:
>
really? iter("SomeString") 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?
Oct 4 '06 #4
mrquantum wrote:
Am Wed, 04 Oct 2006 12:03:41 +0200 schrieb Fredrik Lundh:
>really? iter("SomeString") 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__(self, index):
.... return [3,2,1][index]
....
>>for item in A():
.... print item
....
3
2
1
Know why?
No idea. Perhaps nobody cared?

Peter
Oct 4 '06 #5
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('SomeString') function or the
construction for c in 'SomeString' show.

It's just not implemented by a __iter__ method of the string in question!
Oct 4 '06 #6
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__(self, 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
Oct 4 '06 #7
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('SomeString', '__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

Oct 4 '06 #8

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('SomeString', '__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

Oct 4 '06 #9
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!
Oct 4 '06 #10

"mrquantum" <mr*******@holon.atwrote in message
news:68***************************@news.chello.at. ..
Sorry, was to hasty by saying "... don't support the iteration
protocol'! Sure they do, as the iter('SomeString') 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

Oct 4 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
3247
by: jblazi | last post by:
Let us assume I have a list like and would like to transoform it into the string '{1,2},{7,8},{12,13}' Which is the simplest way of achiebing this? (The list is in fact much longer and...
21
2035
by: Steven Bethard | last post by:
Can someone point me to the documentation on what's supposed to happen when you use the "for x in X:" syntax when X does not have an __iter__ method? I know that the code: >>> class S: .... ...
8
2551
by: Steven Bethard | last post by:
Sorry if this is a repost -- it didn't appear for me the first time. So I was looking at the Language Reference's discussion about emulating container types, and nowhere in it does it mention...
13
1918
by: Thomas Heller | last post by:
I'm trying to implement __iter__ on an abstract base class while I don't know whether subclasses support that or not. Hope that makes sense, if not, this code should be clearer: class Base: def...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.