Wemake-python-styleguide: Forbid a[len(a)-1]

Created on 9 Oct 2019  路  9Comments  路  Source: wemake-services/wemake-python-styleguide

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.

Hacktoberfest help wanted starter rule request

Most helpful comment

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

All 9 comments

Awesome!

@kxepal do you have any comments on this?

Can I take this?

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

Can I take this one?

Done! Thanks! 馃憤

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orsinium picture orsinium  路  3Comments

Dreamsorcerer picture Dreamsorcerer  路  4Comments

Dreamsorcerer picture Dreamsorcerer  路  3Comments

sobolevn picture sobolevn  路  4Comments

sobolevn picture sobolevn  路  4Comments