Git Product home page Git Product logo

Comments (3)

BrunoDesthuilliers avatar BrunoDesthuilliers commented on August 16, 2024

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:

:-)

from python-anti-patterns.

CastixGitHub avatar CastixGitHub commented on August 16, 2024

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.

from python-anti-patterns.

Stigjb avatar Stigjb commented on August 16, 2024

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.

from python-anti-patterns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.