Opened 7 years ago
Closed 6 years ago
#18802 closed enhancement (fixed)
Python 3 preparation: Iterator protocol uses .next() in Py2 but __next__() in Py3
Reported by: | wluebbe | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | sage-7.4 |
Component: | python3 | Keywords: | |
Cc: | Merged in: | ||
Authors: | André Apitzsch | Reviewers: | Frédéric Chapoton |
Report Upstream: | N/A | Work issues: | |
Branch: | 840f968 (Commits, GitHub, GitLab) | Commit: | 840f9686d78fe1a739bec9f9badaf985bba5c341 |
Dependencies: | Stopgaps: |
Description (last modified by )
When defining a custom iterator one has to define a method that returns the next item. In Python 3 this method is called next()
while in Python 3 this is the special method __next__()
.
More information can be found in http://python-future.org/compatible_idioms.html#custom-iterators and http://python-future.org/what_else.html#custom-iterators.
Remark: To advance some iterator it
in Py2 often this method is called directly it.next()
.
But since Python 2.6 there is the builtin function next()
which is compatible between Py2 and Py3: in Py2 it calls the iterator method next()
while in Py3 it calls the special method __next__()
.
Ticket #16075 addresses the (stage 1) conversion from it.next()
to next(it)
.
This ticket is tracked as a dependency of meta-ticket #16052.
Change History (9)
comment:1 Changed 7 years ago by
- Description modified (diff)
comment:2 Changed 7 years ago by
- Description modified (diff)
comment:3 Changed 6 years ago by
- Component changed from misc to python3
comment:4 Changed 6 years ago by
- Branch set to u/aapitzsch/18802
- Commit set to 840f9686d78fe1a739bec9f9badaf985bba5c341
- Milestone changed from sage-6.8 to sage-7.4
- Status changed from new to needs_review
comment:5 follow-up: ↓ 6 Changed 6 years ago by
Are you sure that all these things are really iterators ?
comment:6 in reply to: ↑ 5 Changed 6 years ago by
comment:7 Changed 6 years ago by
I do not quite agree with that. The next methods in permutation and partition are not really iterators (they are implemented on elements, not on a set, and they return False when they cannot return something), and should in fact not be called using next(p) but rather as a method p.next(). Of course, it may be convenient to use next(p) as a shortcut, but this is not the usual semantics of iterators, so I would rather deprecate that.
comment:8 Changed 6 years ago by
- Reviewers set to Frédéric Chapoton
- Status changed from needs_review to positive_review
ok, let us do that, despite the fact that some are not really iterators.
comment:9 Changed 6 years ago by
- Branch changed from u/aapitzsch/18802 to 840f9686d78fe1a739bec9f9badaf985bba5c341
- Resolution set to fixed
- Status changed from positive_review to closed
New commits:
rename next() to __next__() and keep next() as alias