Python type() 関数
type()は、Pythonでオブジェクトの型を取得するための組み込み関数です。
type()は、Pythonで最もよく使われる関数の1つであり、オブジェクトの型を理解するのに役立ちます。動的型付け言語において非常に重要なツールです。
単語の意味: type 意为"类型",用于获取对象的类型信息。
基本構文とパラメータ
構文形式
type(object) type(name, bases, dict)
パラメータの説明
- パラメータ object:
- 型: 任意のオブジェクト
- 説明: 型を取得するオブジェクト。
- パラメータ name, bases, dict:
- 型: 文字列、クラスタプル、辞書
- 説明: クラスを動的に作成するために使用します。
関数の説明
- 戻り値: オブジェクトの型(typeオブジェクト)を返します。
例
例1:オブジェクトの型を取得
例
# 基本型の型を取得
print(type(123)) # 出力: <class 'int'>
print(type(3.14)) # 出力: <class 'float'>
print(type("hello")) # 出力: <class 'str'>
print(type(True)) # 出力: <class 'bool'>
print(type([1, 2, 3])) # 出力: <class 'list'>
print(type({"a": 1})) # 出力: <class 'dict'>
print(type((1, 2))) # 出力: <class 'tuple'>
print(type({1, 2})) # 出力: <class 'set'>
# None の型
print(type(None)) # 出力: <class 'NoneType'>
print(type(123)) # 出力: <class 'int'>
print(type(3.14)) # 出力: <class 'float'>
print(type("hello")) # 出力: <class 'str'>
print(type(True)) # 出力: <class 'bool'>
print(type([1, 2, 3])) # 出力: <class 'list'>
print(type({"a": 1})) # 出力: <class 'dict'>
print(type((1, 2))) # 出力: <class 'tuple'>
print(type({1, 2})) # 出力: <class 'set'>
# None の型
print(type(None)) # 出力: <class 'NoneType'>
想定される実行結果:
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'list'> <class 'dict'> <class 'tuple'> <class 'set'> <class 'NoneType'>
コードの解説:
type()オブジェクトの型オブジェクトを返します。- 各型自体も type クラスのインスタンスです。
例2:型の比較
例
# 直接比較
value = 42
print(type(value) == int) # 出力: True
print(type(value) == str) # 出力: False
# isinstanceを使用(推奨)
print(isinstance(42, int)) # 出力: True
print(isinstance("hello", str)) # 出力: True
print(isinstance(42, (int, float))) # 出力: True
# カスタムクラスを確認
class Dog:
pass
class Cat:
pass
dog = Dog()
print(type(dog) == Dog) # 出力: True
print(type(dog) == Cat) # 出力: False
print(isinstance(dog, Dog)) # 出力: True
value = 42
print(type(value) == int) # 出力: True
print(type(value) == str) # 出力: False
# isinstanceを使用(推奨)
print(isinstance(42, int)) # 出力: True
print(isinstance("hello", str)) # 出力: True
print(isinstance(42, (int, float))) # 出力: True
# カスタムクラスを確認
class Dog:
pass
class Cat:
pass
dog = Dog()
print(type(dog) == Dog) # 出力: True
print(type(dog) == Cat) # 出力: False
print(isinstance(dog, Dog)) # 出力: True
想定される実行結果:
True False True True True True False True
コードの解説:
- 使用できます
type() ==正確な型比較を行います。 - ただし、より推奨されるのは
isinstance()継承をサポートしているためです。
例3:クラスを動的に作成
例
# type() を使用してクラスを動的に作成
Person = type("Person", (), {
"name": "未知",
"age": 0,
"greet": lambda self: fこんにちは、私は {self.name} です
})
# 動的に作成したクラスを使用
p = Person()
p.name = "Tom"
print(p.name) # 出力: Tom
print(p.greet()) # 出力: こんにちは、私は Tom です
# 継承付きのクラスを作成
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "汪汪!"
DogClass = type("DogClass", (Animal,), {
"speak": lambda self: "汪汪!"
})
dog = DogClass()
print(dog.speak()) # 出力: 汪汪!
print(isinstance(dog, Animal)) # 出力: True
Person = type("Person", (), {
"name": "未知",
"age": 0,
"greet": lambda self: fこんにちは、私は {self.name} です
})
# 動的に作成したクラスを使用
p = Person()
p.name = "Tom"
print(p.name) # 出力: Tom
print(p.greet()) # 出力: こんにちは、私は Tom です
# 継承付きのクラスを作成
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "汪汪!"
DogClass = type("DogClass", (Animal,), {
"speak": lambda self: "汪汪!"
})
dog = DogClass()
print(dog.speak()) # 出力: 汪汪!
print(isinstance(dog, Animal)) # 出力: True
想定される実行結果:
動的なクラス作成は実際のプログラミングではあまり使用されませんが、この点を理解することはmetaclassプログラミングにとって重要です。
type()は、Pythonで最も基本的かつ重要な関数の1つです。
その他の拡張
Python3 組み込み関数