Skip to content

Merge is instance

Sourcery refactoring id: merge-is-instance

Description:

Combines together multiple isinstance functions

Before:

if isinstance(hat, Bowler) or isinstance(hat, Fedora):
    wear(hat)

After:

if isinstance(hat, (Bowler, Fedora)):
    wear(hat)

Explanation:

When you want to check whether something is one of multiple different types, you can merge the two isinstance checks into a single call.

This is shorter while staying nice and easy to read.

Back to top