Skip to content

Simplify Generator

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: simplify-generator

Section titled “Sourcery refactoring id: simplify-generator”

An identity generator (a for a in coll) can be replaced directly with the collection coll

if any(hat for hat in wardrobe.hats):
print("I have a hat!")
if any(wardrobe.hats):
print("I have a hat!")

The expression (x for x in y) is a generator that returns all of the elements of y. If being passed into a function like any or all that takes a generator or sequence, it can simply be replaced by y which is much clearer.