Pythonで型を取得・判定するtype関数, isinstance関数
Pythonで、オブジェクトの型を取得して確認したり、特定の型であるかを判定したりするには、組み込み関数type()
やisinstance()
を使う。
オブジェクトの型を判定するのではなく、例外処理を用いたり、組み込み関数hasattr()
であるオブジェクトが正しいメソッドや属性を持っているかを判定したりする方法もある。
クラスの継承関係を判定したり、サブクラス・スーパークラスの一覧を取得する方法については以下の記事を参照。
オブジェクトの型を取得・確認: type()関数
組み込み関数type()
は引数に渡したオブジェクトの型を返す。これを使ってオブジェクトの型を確認できる。
print(type('string'))
# <class 'str'>
print(type(100))
# <class 'int'>
print(type([0, 1, 2]))
# <class 'list'>
type()
の返り値はstr
やint
などのtype
型(型オブジェクト)。
print(type(type('string')))
# <class 'type'>
print(type(str))
# <class 'type'>
オブジェクトの型の判定: type()関数, isinstance()関数
型の判定にはtype()
かisinstance()
を使う。
type()を使った型の判定
type()
の返り値と任意の型を比較することで、そのオブジェクトが任意の型であるかを判定できる。
print(type('string') is str)
# True
print(type('string') is int)
# False
def is_str(v):
return type(v) is str
print(is_str('string'))
# True
print(is_str(100))
# False
print(is_str([0, 1, 2]))
# False
複数の型のいずれかかどうかを判定するには、in
演算子と複数の型のタプルやリストを使う。
def is_str_or_int(v):
return type(v) in (str, int)
print(is_str_or_int('string'))
# True
print(is_str_or_int(100))
# True
print(is_str_or_int([0, 1, 2]))
# False
引数の型によって処理を変える関数を定義することも可能。
def type_condition(v):
if type(v) is str:
print('type is str')
elif type(v) is int:
print('type is int')
else:
print('type is not str or int')
type_condition('string')
# type is str
type_condition(100)
# type is int
type_condition([0, 1, 2])
# type is not str or int
isinstance()を使った型の判定
組み込み関数isinstance()
は、第一引数のオブジェクトが、第二引数の型(またはそのサブクラス)のインスタンスであればTrue
を返す。
第二引数には型のタプルを指定することも可能。いずれかの型(またはそのサブクラス)のインスタンスであればTrue
を返す。
print(isinstance('string', str))
# True
print(isinstance(100, str))
# False
print(isinstance(100, (int, str)))
# True
type()
を使った型の判定の例と同様の関数は以下のように書ける。
def is_str(v):
return isinstance(v, str)
print(is_str('string'))
# True
print(is_str(100))
# False
print(is_str([0, 1, 2]))
# False
def is_str_or_int(v):
return isinstance(v, (int, str))
print(is_str_or_int('string'))
# True
print(is_str_or_int(100))
# True
print(is_str_or_int([0, 1, 2]))
# False
def type_condition(v):
if isinstance(v, str):
print('type is str')
elif isinstance(v, int):
print('type is int')
else:
print('type is not str or int')
type_condition('string')
# type is str
type_condition(100)
# type is int
type_condition([0, 1, 2])
# type is not str or int
type()とisinstance()の違い
type()
とisinstance()
の違いは、isinstance()
は第二引数に指定したクラスを継承するサブクラスのインスタンスに対してもTrue
を返すという点。
例えば、以下のようなスーパークラス(基底クラス・親クラス)とサブクラス(派生クラス・子クラス)を定義する。
class Base:
pass
class Derive(Base):
pass
base = Base()
print(type(base))
# <class '__main__.Base'>
derive = Derive()
print(type(derive))
# <class '__main__.Derive'>
type()
を使った型の判定では型が一致しているときのみTrue
となるが、isinstance()
ではスーパークラスに対してもTrue
を返す。
print(type(derive) is Derive)
# True
print(type(derive) is Base)
# False
print(isinstance(derive, Derive))
# True
print(isinstance(derive, Base))
# True
標準の型でも、例えば真偽値型bool
(True
, False
)は注意が必要。bool
は整数型int
のサブクラスなので、isinstance()
では継承元のint
に対してもTrue
を返す。
print(type(True))
# <class 'bool'>
print(type(True) is bool)
# True
print(type(True) is int)
# False
print(isinstance(True, bool))
# True
print(isinstance(True, int))
# True
正確な型を判定したい場合はtype()
、継承も考慮した上で判定したい場合はisinstance()
を使えばよい。
なお、あるクラスがあるクラスのサブクラスであるかを判定する組み込み関数issubclass()
も提供されている。
print(issubclass(bool, int))
# True
print(issubclass(bool, float))
# False
サブクラス・スーパークラスの確認方法については以下の記事を参照。