bit-count (FURB161)
Derived from the refurb linter.
Fix is always available.
What it does
Checks for uses of bin(...).count("1")
to perform a population count.
Why is this bad?
In Python 3.10, a bit_count()
method was added to the int
class,
which is more concise and efficient than converting to a binary
representation via bin(...)
.
Example
Use instead:
Fix safety
This rule's fix is marked as unsafe unless the argument to bin
can be inferred as
an instance of a type that implements the __index__
and bit_count
methods because this can
change the exception raised at runtime for an invalid argument.