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

Modification to "pairwise" in itertools recipes #49600

Closed
della mannequin opened this issue Feb 23, 2009 · 7 comments
Closed

Modification to "pairwise" in itertools recipes #49600

della mannequin opened this issue Feb 23, 2009 · 7 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@della
Copy link
Mannequin

della mannequin commented Feb 23, 2009

BPO 5350
Nosy @birkenfeld, @rhettinger

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 = 'https://github.com/rhettinger'
closed_at = <Date 2009-02-23.19:40:08.891>
created_at = <Date 2009-02-23.13:37:11.058>
labels = ['docs']
title = 'Modification to "pairwise" in itertools recipes'
updated_at = <Date 2009-02-23.21:57:27.667>
user = 'https://bugs.python.org/della'

bugs.python.org fields:

activity = <Date 2009-02-23.21:57:27.667>
actor = 'della'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2009-02-23.19:40:08.891>
closer = 'rhettinger'
components = ['Documentation']
creation = <Date 2009-02-23.13:37:11.058>
creator = 'della'
dependencies = []
files = []
hgrepos = []
issue_num = 5350
keywords = []
message_count = 7.0
messages = ['82626', '82627', '82630', '82638', '82640', '82641', '82646']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'rhettinger', 'LambertDW', 'della']
pr_nums = []
priority = 'normal'
resolution = 'accepted'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue5350'
versions = []

@della
Copy link
Mannequin Author

della mannequin commented Feb 23, 2009

I feel that the "pairwise" recipe could be slightly more elegant if "for
elem in b: break" became a simpler next(b) (or b.next() for Python 2.x).
It is also more natural to modify the recipes to suit one's needs (e.g.,
returning items with a given gap between them, or convert the recipe to
k-wise iteration).

@della della mannequin assigned birkenfeld Feb 23, 2009
@della della mannequin added the docs Documentation in the Doc dir label Feb 23, 2009
@birkenfeld
Copy link
Member

Assigning to Raymond.

Note that "for elem in b: break" and "next(b)" are not equivalent when b
is empty/exhausted.

@birkenfeld birkenfeld assigned rhettinger and unassigned birkenfeld Feb 23, 2009
@della
Copy link
Mannequin Author

della mannequin commented Feb 23, 2009

Georg, you're right, there's a StopIteration to catch. My thinko was
mistaking the function for a generator where the exception propagation
would have done the right thing. The amended version now becomes

next(b)
for x, y in zip(a, b): yield x, y

...which is not that attractive anymore, also because it's slower. Sorry
for the error.

@rhettinger
Copy link
Contributor

Applied in r69908 .

@lambertdw
Copy link
Mannequin

lambertdw mannequin commented Feb 23, 2009

Nice. I had thought of this a while ago but found counter example,
probably using the empty iterator

def f():
    raise StopIteration
    yield

I didn't realize "next" had optional argument.

@birkenfeld
Copy link
Member

Shame on me, I forgot about the optional argument too.

@della
Copy link
Mannequin Author

della mannequin commented Feb 23, 2009

great Raymond! :)

@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
Projects
None yet
Development

No branches or pull requests

2 participants