Order by before where (ARCH2076)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key python.performance.orderby-before-where · Severity warning · Category performance
Sorting before filtering wastes work by ordering elements that will be discarded. Filter first, then sort the smaller remaining set.
Incorrect
Section titled “Incorrect”def top_active(users): ordered = sorted(users, key=lambda u: u.score, reverse=True) return [u for u in ordered if u.active][:10]Correct
Section titled “Correct”def top_active(users): active = [u for u in users if u.active] return sorted(active, key=lambda u: u.score, reverse=True)[:10]Filter first and apply the sort only to the smaller remaining set.