logo

Python hash() メソッド

Python ハッシュ() 関数 は組み込み関数であり、オブジェクトにハッシュ値がある場合はそのハッシュ値を返します。ハッシュ値は、辞書のキーを素早く比較するために使用される整数です。 辞書

Python hash() 関数の構文

構文: ハッシュ(オブジェクト)



パラメータ:obj: ハッシュに変換する必要があるオブジェクト。

戻り値 : 可能であればハッシュ値を返します。

hash() 関数のプロパティ

  • hash() を使用してハッシュされたオブジェクトは元に戻せないため、情報が失われます。
  • hash() は不変オブジェクトのハッシュ値のみを返すため、変更可能/不変オブジェクトをチェックする指標として使用できます。
  • hash() 関数を使用して、Python でセキュリティのためにデータをエンコードできます。

Python hash() 関数の例

例 1: hash() の動作のデモ

この例では、 hash() 関数を使用して、整数、文字列、浮動小数点数のハッシュ値を出力しています。 パイソン



Python3






# initializing objects> int_val>=> 4> str_val>=> 'techcodeview.com'> flt_val>=> 24.56> # Printing the hash values.> # Notice Integer value doesn't change> # You'll have answer later in article.> print>(>'The integer hash value is : '> +> str>(>hash>(int_val)))> print>(>'The string hash value is : '> +> str>(>hash>(str_val)))> print>(>'The float hash value is : '> +> str>(>hash>(flt_val)))>

>

Linux コマンド
>

出力

The integer hash value is : 4 The string hash value is : 4349415460800802357 The float hash value is : 1291272085159665688>

例 2: hash() のプロパティのデモ

この Python コードは、hash()>関数は次のように動作します 不変オブジェクトと可変オブジェクト 。まず、同じ要素を持つタプルとリストを初期化します。コードはタプルのハッシュ値を正常に出力します。ただし、変更可能なリストのハッシュ値を出力しようとすると、TypeError>リストのような可変オブジェクトは直接ハッシュできないためです。

Python3




# initializing objects> # tuple are immutable> tuple_val>=> (>1>,>2>,>3>,>4>,>5>)> # list are mutable> list_val>=> [>1>,>2>,>3>,>4>,>5>]> # Printing the hash values.> # Notice exception when trying> # to convert mutable object> print>(>'The tuple hash value is : '> +> str>(>hash>(tuple_val)))> print>(>'The list hash value is : '> +> str>(>hash>(list_val)))>

>

>

出力:

Javaの数値を文字列に変換
The tuple hash value is : 8315274433719620810>

例外:

Traceback (most recent call last):  File '/home/eb7e39084e3d151114ce5ed3e43babb8.py', line 15, in   print ('The list hash value is : ' + str(hash(list_val))) TypeError: unhashable type: 'list'>

例 3: 不変タプル オブジェクトの hash()

この Python コードは、という名前のタプルを初期化します。var>「G」、「E」、「E」、「K」の文字が含まれています。のhash()>関数はタプルのハッシュ値を生成するために使用されます。var>。この場合、コードはタプルのハッシュ値を出力します。これは、内容と構造に基づいてタプルを表す一意の整数です。

Python3




# hash() for immutable tuple object> var>=> (>'G'>,>'E'>,>'E'>,>'K'>)> print>(>hash>(var))>

>

>

出力

3843673861956289951>

例 4: hash() の場合 彼ら 使用可能なオブジェクト

hash() メソッドは 1 つの不変オブジェクトで使用されますが、リスト、セット、辞書などの変更可能なオブジェクトでこれを使用すると、エラーが生成されます。

Python3




l>=> [>1>,>2>,>3>,>4>]> print>(>hash>(l))>

>

sqrt Java数学

>

出力

TypeError: unhashable type: 'list'>

例 5: カスタム オブジェクトの hash()

ここでは、__hash()__ メソッドをオーバーライドして hash() を呼び出し、__eq__() メソッドは 2 つのカスタム オブジェクトの同等性をチェックします。

Python3




class> Emp:> >def> __init__(>self>, emp_name,>id>):> >self>.emp_name>=> emp_name> >self>.>id> => id> >def> __eq__(>self>, other):> > ># Equality Comparison between two objects> >return> self>.emp_name>=>=> other.emp_name>and> self>.>id> =>=> other.>id> >def> __hash__(>self>):> > ># hash(custom_object)> >return> hash>((>self>.emp_name,>self>.>id>))> emp>=> Emp(>'Ragav'>,>12>)> print>(>'The hash is: %d'> %> hash>(emp))> # We'll check if two objects with the same> # attribute values have the same hash> emp_copy>=> Emp(>'Ragav'>,>12>)> print>(>'The hash is: %d'> %> hash>(emp_copy))>

>

>

出力

The hash is: 7627717261227283506 The hash is: 7627717261227283506>