Skip to content

Forbid a[len(a)-1] #862

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

Closed
orsinium opened this issue Oct 9, 2019 · 9 comments
Closed

Forbid a[len(a)-1] #862

orsinium opened this issue Oct 9, 2019 · 9 comments
Assignees
Labels
Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Milestone

Comments

@orsinium
Copy link
Collaborator

orsinium commented Oct 9, 2019

Rule request

Thesis

That's a common mistake of gophers. Golang has no negative index (yet?), but Python has.

Bad:

a[len(a) - 1]
a[len(a) + 1]  # IndexError

Good:

a[-1]
a[1]
a[len(a) // 2]

The floor division is ok. Plus len and minus len isn't

Reasoning

a[len(a)-1] is slow, a[len(a)+1] is IndexError.

@orsinium orsinium added the rule request Adding a new rule label Oct 9, 2019
@sobolevn
Copy link
Member

sobolevn commented Oct 9, 2019

Awesome!

@sobolevn sobolevn added this to the Version 0.13 milestone Oct 9, 2019
@sobolevn sobolevn added Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers labels Oct 9, 2019
@sobolevn
Copy link
Member

sobolevn commented Oct 9, 2019

@kxepal do you have any comments on this?

@ghost
Copy link

ghost commented Oct 13, 2019

Can I take this?

@sobolevn
Copy link
Member

Let's start with #861
Later you can take as many as you want 🙂

@lensvol
Copy link
Collaborator

lensvol commented Oct 14, 2019

Can I take this one?

@sobolevn
Copy link
Member

Done! Thanks! 👍

@kxepal
Copy link

kxepal commented Oct 14, 2019

do you have any comments on this?

LGFM, except this case is quite rare one and mostly relates to migration from some Golang to Python. I can't find any similar issues in real Python code.

@sobolevn
Copy link
Member

sobolevn commented Nov 4, 2019

Hi @lensvol, how's it going? Do you need any help?

@lensvol
Copy link
Collaborator

lensvol commented Nov 4, 2019

Hi @sobolevn, everything is on track, got distracted by real-life issues sadly :/ Hope I will close it this week.

@sobolevn sobolevn assigned sobolevn and unassigned lensvol Nov 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest Hactoberfest fun! help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

4 participants