use isinstance instead of type

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

Metadata

ID: python-best-practices/type-check-isinstance

Language: Python

Severity: Notice

Category: Best Practices

Description

Using isinstance is faster than type but also consider inheritance, which makes it more accurate.

Non-Compliant Code Examples

# use isinstance instead of
if type(Foo()) == Foo:
    print("is foo")

Compliant Code Examples

raise ValueError("target %s config %s has type of %s" % (target, config_content, type(config_content)))
if isinstance(Bar(), Foo):
    print("foo")
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis