Opened 5 years ago

Closed 3 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) Commit: 840f9686d78fe1a739bec9f9badaf985bba5c341
Dependencies: Stopgaps:

Description (last modified by wluebbe)

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 and

Remark: To advance some iterator it in Py2 often this method is called directly 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 to next(it).

This ticket is tracked as a dependency of meta-ticket #16052.

Change History (9)

comment:1 Changed 5 years ago by wluebbe

  • Description modified (diff)

comment:2 Changed 5 years ago by wluebbe

  • Description modified (diff)

comment:3 Changed 4 years ago by jdemeyer

  • Component changed from misc to python3

comment:4 Changed 3 years ago by aapitzsch

  • Authors set to André Apitzsch
  • 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

New commits:

840f968rename next() to __next__() and keep next() as alias

comment:5 follow-up: Changed 3 years ago by chapoton

Are you sure that all these things are really iterators ?

comment:6 in reply to: ↑ 5 Changed 3 years ago by aapitzsch

Replying to chapoton:

Are you sure that all these things are really iterators ?


comment:7 Changed 3 years ago by chapoton

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 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 3 years ago by chapoton

  • 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 3 years ago by vbraun

  • Branch changed from u/aapitzsch/18802 to 840f9686d78fe1a739bec9f9badaf985bba5c341
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.