Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document whether strings implement __iter__ #58733

Closed
cjerdonek opened this issue Apr 8, 2012 · 7 comments
Closed

Document whether strings implement __iter__ #58733

cjerdonek opened this issue Apr 8, 2012 · 7 comments
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@cjerdonek
Copy link
Member

BPO 14528
Nosy @birkenfeld, @rhettinger, @cjerdonek

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2012-04-08.20:13:20.249>
created_at = <Date 2012-04-08.10:05:37.411>
labels = ['type-feature', 'invalid', 'docs']
title = 'Document whether strings implement __iter__'
updated_at = <Date 2012-04-08.22:05:12.270>
user = 'https://github.com/cjerdonek'

bugs.python.org fields:

activity = <Date 2012-04-08.22:05:12.270>
actor = 'chris.jerdonek'
assignee = 'docs@python'
closed = True
closed_date = <Date 2012-04-08.20:13:20.249>
closer = 'rhettinger'
components = ['Documentation']
creation = <Date 2012-04-08.10:05:37.411>
creator = 'chris.jerdonek'
dependencies = []
files = []
hgrepos = []
issue_num = 14528
keywords = []
message_count = 7.0
messages = ['157783', '157784', '157785', '157786', '157787', '157808', '157812']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'rhettinger', 'chris.jerdonek', 'docs@python']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue14528'
versions = ['Python 2.6', 'Python 3.1', 'Python 2.7', 'Python 3.2', 'Python 3.3', 'Python 3.4']

@cjerdonek
Copy link
Member Author

While converting code from Python 2 to Python 3, I came across the "gotcha" that strings implement __iter__ in Python 3 but not in Python 2.

Looking through the documentation, I don't seem to see anything like this mentioned in the library portion of either Python 2 or 3's documentation:

http://docs.python.org/library/stdtypes.html#iterator-types
http://docs.python.org/py3k/library/stdtypes.html#iterator-types

Or in the documentation describing differences between 2 and 3:

http://docs.python.org/release/3.0.1/whatsnew/3.0.html

In fact, the Python 2 and 3 sections on iterator types seem largely the same. Python 2's documentation even seems a bit misleading in this regard. At the beginning of this section, it says, "Sequences, described below in more detail, always support the iteration methods [of which __iter__() is the main one]." And str and unicode are the first two types mentioned in that next section on sequence types.

Here is a blog post I came across about this issue:

http://plope.com/Members/chrism/python_2_vs_python_3_str_iter

I think it would be worth highlighting this issue somewhere in the Python documentation, or at least acknowledging the change (unless I'm simply looking in the wrong place, in which case maybe it should be made more visible).

@cjerdonek cjerdonek added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Apr 8, 2012
@birkenfeld
Copy link
Member

Why is it so important if strings implement __iter__? They are iterable in both versions, since iteration falls back on __getitem__ if no __iter__ is defined. For user code it is irrelevant which of the iteration protocols is present.

@cjerdonek
Copy link
Member Author

It is not "so important." I just feel that the change should be acknowledged somewhere -- insofar as the existing user documentation on iterator types already discusses __iter__(). As it stands now, the Python 2 documentation is a bit misleading because it seems to suggest that strings implement __iter__().

With regard to falling back to __getitem__(), that might actually be worth mentioning in the section on iterator types. Up until today, I didn't know there was a distinction between a "sequence protocol" and an "iterator protocol," as discussed here, for example--

http://blog.axant.it/archives/306

For user code, the user might want different behavior depending on whether something behaves like a list. For that, they might be relying on something like the presence of __iter__().

@birkenfeld
Copy link
Member

"behaves like a list" is misleading. If you mean checking for iterable-ness, calling iter() on the object is the way to do it.

@cjerdonek
Copy link
Member Author

Okay, then that also might be worth mentioning. As it stands now, the emphasis in the section on iterator types is on __iter__() (e.g. it is the main focus of the introduction), whereas iter() is barely mentioned (only in the sections on dicts and file objects). So in addition to the suggestions above, perhaps the introduction to the section on iterator types could include a link to the iter() function with a description of its relationship to iterator types.

@rhettinger
Copy link
Contributor

Sorry, I agree with Georg, this isn't a bug, not even a documentation bug. A type is free to implement iteration either by way of __iter__ or by way of __getitem__. How it chooses to do so is an implementation detail (and different implementations have made different choices).

@cjerdonek
Copy link
Member Author

Is there a mechanism for suggesting improvements to the documentation (e.g. for pedagogical reasons)? I tried to classify this as an enhancement request rather than as a bug.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants