How to make Python interpreter a little more strict?

Chris Angelico rosuav at gmail.com
Sat Mar 26 08:05:38 EDT 2016


On Fri, Mar 25, 2016 at 11:06 PM, Aleksander Alekseev <afiskon at devzen.ru> wrote:
> Recently I spend half an hour looking for a bug in code like this:
>
> eax at fujitsu:~/temp$ cat ./t.py
> #!/usr/bin/env python3
>
> for x in range(0,5):
>     if x % 2 == 0:
>         next
>     print(str(x))
>
> eax at fujitsu:~/temp$ ./t.py
> 0
> 1
> 2
> 3
> 4
>
> Is it possible to make python complain in this case? Or maybe solve
> such an issue somehow else?

I think what you're looking for here is an acknowledgement that
evaluating the name "next" accomplishes nothing. That's not really
something the Python interpreter should be looking at (hey, you might
have good reason for doing that), but there are linters that can
detect this kind of dead code. Some of them tie into programmer's
editors, so you could get a nice little warning message right in the
window where you're typing your code. Look into some of the top-end
editors (free or commercial) and see what you think of them - they can
save you no end of time.

ChrisA



More information about the Python-list mailing list