Python

Python

By DeepSource

Consider merging startswith/endswith checks PY-W0077

Anti-pattern

The methods startswith and endswith for string instances accept two types of Python constructs as the first argument. The types are:

  • a Python string, or
  • a tuple of strings

It is recommended to merge the arguments of multiple calls to startswith/endswith connected by or.

Example

Bad practice

if s.startswith('a') or s.startswith('b'):
    ...

Recommended

if s.startswith(('a', 'b')):
    ...