type() 関数は主にデバッグ目的で使用されます。 type() 関数には、1 つの引数と 3 つの引数という 2 つの異なるタイプの引数を渡すことができます。単一の引数 type(obj) が渡された場合、指定されたオブジェクトの型を返します。 3 つの引数の型 (object、bases、dict) が渡されると、新しい型のオブジェクトが返されます。
Python type() 関数の構文
構文: type(オブジェクト、ベース、辞書)
パラメーター :
- 物体: 必須。パラメータが 1 つだけ指定されている場合、type() 関数はこのオブジェクトのタイプを返します。
- ベース: 現在のクラスの派生元となるクラスのタプル。後は __bases__ 属性に対応します。
- 辞書: クラスの名前空間を保持する辞書。後は __dict__ 属性に対応します。
戻る: 新しい型クラス、または基本的にメタクラスを返します。
type() 関数の仕組み Pythonで?
与えられた例では、変数 x の型を出力しています。 Python でオブジェクトの型を決定します。
Python3
クイックソートアルゴリズム
x> => 10> print> (> type> (x))> |
>
>出力
>
Python の type() 関数の例
type() 関数を使用すると、Python でオブジェクトの型を決定できます。以下は、type() 関数に関連するその他の例です。
Python オブジェクトの型を調べる
ここでは、type() 関数を使用してオブジェクトの型をチェックしています。 パイソン 。
Python3
a> => (> 'Geeks'> ,> 'for'> ,> 'Geeks'> )> b> => [> 'Geeks'> ,> 'for'> ,> 'Geeks'> ]> c> => {> 'Geeks'> :> 1> ,> 'for'> :> 2> ,> 'Geeks'> :> 3> }> d> => 'Hello World'> e> => 10.23> f> => 11.22> print> (> type> (a))> print> (> type> (b))> print> (> type> (c))> print> (> type> (d))> print> (> type> (e))> print> (> type> (f))> |
>
>出力
Javaのカスタマイズされた例外
>
オブジェクトが Python のタイプであるかどうかを確認する
この例では、条件を使用してオブジェクトをテストし、ブール値を出力します。
Python3
print> (> type> ([])> is> list> )> print> (> type> ([])> is> not> list> )> print> (> type> (())> is> tuple> )> print> (> type> ({})> is> dict> )> print> (> type> ({})> is> not> list> )> |
>
マイフリックス
>出力
True False True True True>
条件文での type() の使用
この例では、type() 関数を使用して、条件付き if-else ステートメントで Python のオブジェクトの型を決定しています。
Python3
# Example variables> my_tuple> => (> 10> ,> 'Hello'> ,> 45> ,> 'Hi'> )> my_dict> => {> 1> :> 'One'> ,> 2> :> 'Two'> ,> 3> :> 'Three'> }> # Check if the variables have the same object type> if> type> (my_tuple)> is> not> type> (my_dict):> > print> (> 'The variables have different object types.'> )> else> :> > print> (> 'The variables have the same object type.'> )> |
>
>出力
The variables have different object types.>
3 つのパラメータを持つ Python type()
この例では、 クラス 基本クラスと基本クラスから派生したクラスはありません。 type() 関数を使用すると、実行時にクラスとその属性をプログラムで定義できます。
Python3
# New class(has no base) class with the> # dynamic class initialization of type()> new> => type> (> 'New'> , (> object> , ),> > dict> (var1> => 'techcodeview.com'> , b> => 2009> ))> # Print type() which returns class 'type'> print> (> type> (new))> print> (> vars> (new))> # Base class, incorporated> # in our new class> class> test:> > a> => 'Geeksforgeeks'> > b> => 2009> # Dynamically initialize Newer class> # It will derive from the base class test> newer> => type> (> 'Newer'> , (test, ),> > dict> (a> => 'Geeks'> , b> => 2018> ))> print> (> type> (newer))> print> (> vars> (newer))> |
>
Javaでこんにちは世界
>
出力
{‘var1’: ‘techcodeview.com’, ‘b’: 2009, ‘__module__’: ‘__main__’, ‘__dict__’: , ‘__weakref__’: , ‘__doc__’: なし}
{‘a’: ‘Geeks’, ‘b’: 2018, ‘__module__’: ‘__main__’, ‘__doc__’: なし}
Python type() 関数の応用
- タイプ( ) 関数は基本的にデバッグ目的で使用されます。 Web クローラーから抽出されたテキストで .upper()、. lower()、.split() などの他の文字列関数を使用する場合、文字列関数をサポートしていない異なる型である可能性があるため、機能しない可能性があります。その結果、デバッグが非常に困難なエラーがスローされ続けます [GeneratorType には lower() 属性がないため、エラーを考慮してください]。
- タイプ() この時点で関数を使用すると、抽出されたテキストの種類を判断し、文字列関数やその他の操作を使用する前に、それを他の形式の文字列に変更できます。
- タイプ() 3 つの引数を指定した を使用すると、クラスまたは属性を持つ既存のクラスを動的に初期化できます。データベーステーブルを登録するためにも使用されます。 SQL 。
- 単体テストフレームワークでは、
type()>
関数またはメソッドの出力を検証するために使用でき、期待されるデータ型が返されることを確認できます。