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

Python 3 preparation: Iterator protocol uses .next() in Py2 but __next__() in Py3 #18802

Closed
wluebbe mannequin opened this issue Jun 28, 2015 · 12 comments
Closed

Python 3 preparation: Iterator protocol uses .next() in Py2 but __next__() in Py3 #18802

wluebbe mannequin opened this issue Jun 28, 2015 · 12 comments

Comments

@wluebbe
Copy link
Mannequin

wluebbe mannequin commented Jun 28, 2015

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.

Component: python3

Author: André Apitzsch

Branch/Commit: 840f968

Reviewer: Frédéric Chapoton

Issue created by migration from https://trac.sagemath.org/ticket/18802

@wluebbe wluebbe mannequin added this to the sage-6.8 milestone Jun 28, 2015
@wluebbe wluebbe mannequin added p: major / 3 labels Jun 28, 2015
@wluebbe

This comment has been minimized.

@wluebbe

This comment has been minimized.

@a-andre
Copy link

a-andre commented Sep 4, 2016

Branch: u/aapitzsch/18802

@a-andre
Copy link

a-andre commented Sep 4, 2016

Author: André Apitzsch

@a-andre
Copy link

a-andre commented Sep 4, 2016

Commit: 840f968

@a-andre
Copy link

a-andre commented Sep 4, 2016

New commits:

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

@a-andre a-andre modified the milestones: sage-6.8, sage-7.4 Sep 4, 2016
@fchapoton
Copy link
Contributor

comment:5

Are you sure that all these things are really iterators ?

@a-andre
Copy link

a-andre commented Sep 4, 2016

comment:6

Replying to @fchapoton:

Are you sure that all these things are really iterators ?

Yes.

@fchapoton
Copy link
Contributor

comment:7

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.

@fchapoton
Copy link
Contributor

Reviewer: Frédéric Chapoton

@fchapoton
Copy link
Contributor

comment:8

ok, let us do that, despite the fact that some are not really iterators.

@vbraun
Copy link
Member

vbraun commented Sep 12, 2016

Changed branch from u/aapitzsch/18802 to 840f968

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants