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

add methods to get first and last elements of a range #91413

Closed
phr mannequin opened this issue Apr 8, 2022 · 3 comments
Closed

add methods to get first and last elements of a range #91413

phr mannequin opened this issue Apr 8, 2022 · 3 comments
Labels
type-feature A feature request or enhancement

Comments

@phr
Copy link
Mannequin

phr mannequin commented Apr 8, 2022

BPO 47257
Nosy @mdickinson

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 2022-04-08.10:34:44.377>
created_at = <Date 2022-04-08.08:03:40.803>
labels = ['type-feature']
title = 'add methods to get first and last elements of a range'
updated_at = <Date 2022-04-08.10:34:44.376>
user = 'https://bugs.python.org/phr'

bugs.python.org fields:

activity = <Date 2022-04-08.10:34:44.376>
actor = 'phr'
assignee = 'none'
closed = True
closed_date = <Date 2022-04-08.10:34:44.377>
closer = 'phr'
components = []
creation = <Date 2022-04-08.08:03:40.803>
creator = 'phr'
dependencies = []
files = []
hgrepos = []
issue_num = 47257
keywords = []
message_count = 3.0
messages = ['416962', '416971', '416973']
nosy_count = 2.0
nosy_names = ['phr', 'mark.dickinson']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue47257'
versions = []

@phr
Copy link
Mannequin Author

phr mannequin commented Apr 8, 2022

Inspired by a question on comp.lang.python about how to deal with an int set composed of integers and ranges. Range objects like range(1,5,2) contain start, stop, and step values, but it's messy and potentially tricky to get the actual first and last values of the range. Examples:

range(1,5,2) - first = 1, last = 3

range (5, 1, 2) - range is empty, first = last = None

range(5, 1, -1) - first is 5, last is 2

Note in the case where the range is not empty, you can get the "last" by a messy calculation but it's easier to pick the first element from the reverse iterator. But then you might forget to catch the stopiteration exception in the case that the list is empty. The same goes for the first element, roughly. And constructing the iterators just to pick one element seems like unnecessary overhead.

So it is better to have actual methods for these, with type Optional[int]. Then mypy should remind you to check for the empty case if you forget.

@phr phr mannequin added type-feature A feature request or enhancement labels Apr 8, 2022
@mdickinson
Copy link
Member

but it's messy and potentially tricky to get the actual first and last values of the range

Doesn't simple indexing already provide what you need here?

>>> range(1, 5, 2)[0]  # first element of range
1
>>> range(1, 5, 2)[-1]  # last element of range
3

@phr
Copy link
Mannequin Author

phr mannequin commented Apr 8, 2022

Oh nice, I didn't realize you could do that. len(range) and bool(range) (to test for empty) also work. Ok I guess this enhancement is not needed. I will close ticket, hope that is procedurally correct, otherwise feel free to fix. Thanks.

@phr phr mannequin closed this as completed Apr 8, 2022
@phr phr mannequin closed this as completed Apr 8, 2022
@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
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant