Repeated work in loop (ARCH2072)
Ce contenu n’est pas encore disponible dans votre langue.
Key python.performance.repeated-work-in-loop · Severity info · Category performance
A call or computation that does not depend on the loop variable wastes work when re-evaluated every iteration. Hoist the invariant expression out of the loop.
Incorrect
Section titled “Incorrect”def filter_active(rows, config): result = [] for row in rows: if row.status == config.get_active_status().upper(): result.append(row) return resultCorrect
Section titled “Correct”def filter_active(rows, config): active = config.get_active_status().upper() return [row for row in rows if row.status == active]Hoist the loop-invariant computation into a variable before the loop.