.. _use-a-generator:
use-a-generator / R1729
=======================
**Message emitted:**
``Use a generator instead '%s(%s)'``
**Description:**
*Comprehension inside of 'any', 'all', 'max', 'min' or 'sum' is unnecessary. A generator would be sufficient and faster.*
**Problematic code:**
.. literalinclude:: /data/messages/u/use-a-generator/bad.py
:language: python
**Correct code:**
.. literalinclude:: /data/messages/u/use-a-generator/good.py
:language: python
**Additional details:**
By using a generator you can cut the execution tree and exit directly at the first element that is ``False`` for ``all`` or ``True`` for ``any`` instead of
calculating all the elements. Except in the worst possible case where you still need to evaluate everything (all values
are True for ``all`` or all values are false for ``any``) performance will be better.
**Related links:**
- `PEP 289 – Generator Expressions `_
- `Benchmark and discussion during initial implementation `_
Created by the `refactoring `__ checker.