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

built-in overriden in "correctness -> else clause on loop without a break statement" #100

Open
CastixGitHub opened this issue Apr 16, 2017 · 3 comments

Comments

@CastixGitHub
Copy link

The list built-in is overridden
Thanks for your beautiful work :)

@BrunoDesthuilliers
Copy link

in https://docs.quantifiedcode.com/python-anti-patterns/correctness/else_clause_on_loop_without_a_break_statement.html :

def contains_magic_number(list, magic_number):
    for i in list:

:-)

@CastixGitHub CastixGitHub changed the title paragraph 1.6 does not respect 1.3 built-in overriden in "correctness -> else clause on loop without a break statement" Jan 6, 2018
@CastixGitHub
Copy link
Author

Talking about correctness, instead of break, I think return is more appropriate here

>>> def contains_magic_number(numbers, magic_number):
...     for n in numbers:
...             if magic_number == n:
...                     return True
...     else:
...             return False
... 
>>> print("This list contains the magic number."
...       if contains_magic_number(range(10), 5)
...       else "This list does NOT contain the magic number.")
This list contains the magic number

otherwise do not use a loop at all, use a list comprehension or filter() instead

>>> def contains_magic_number(numbers, magic_number):
...     if len([n for n in numbers if n == magic_number]) != 0:
...             print("This list contains the magic number.")
...     else:
...             print("This list does NOT contain the magic number.")
... 
>>> contains_magic_number(range(10), 5)
This list contains the magic number.

@Stigjb
Copy link

Stigjb commented Aug 27, 2019

This seems to have been fixed in 5f6e7bf, so the issue should probably be closed.

However, the website still shows the old version that redefines list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants