تخطَّ إلى المحتوى

Repeated work in loop (ARCH2072)

هذا المحتوى غير متوفر بلغتك بعد.

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.

def filter_active(rows, config):
result = []
for row in rows:
if row.status == config.get_active_status().upper():
result.append(row)
return result
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.