Python hash() funkcija je vgrajena funkcija in vrne zgoščeno vrednost predmeta, če jo ima. Zgoščena vrednost je celo število, ki se uporablja za hitro primerjavo slovarskih ključev, medtem ko gledate a slovar .
Sintaksa funkcije Python hash().
Sintaksa: hash(obj)
Parametri: obj: Objekt, ki ga moramo pretvoriti v hash.
Vrne: Če je mogoče, vrne zgoščeno vrednost.
Lastnosti funkcije hash().
- Objekti, zgoščeni s funkcijo hash(), so nepovratni, kar povzroči izgubo informacij.
- hash() vrne zgoščeno vrednost samo za nespremenljive predmete, zato se lahko uporablja kot indikator za preverjanje spremenljivih/nespremenljivih objektov.
- Podatke lahko za varnost v Pythonu kodiramo z uporabo funkcije hash().
Primeri funkcij Python hash().
Primer 1: Prikaz delovanja hash()
V tem primeru uporabljamo funkcijo hash() za tiskanje zgoščene vrednosti celega števila, niza in plavajoče vrednosti z uporabo hash() v Python .
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)))> |
>
>Izhod
The integer hash value is : 4 The string hash value is : 4349415460800802357 The float hash value is : 1291272085159665688>
Primer 2: Prikaz lastnosti hash()
Ta koda Python prikazuje, kakohash()>funkcija se obnaša z nespremenljivi in spremenljivi predmeti . Najprej inicializira tuple in seznam z istimi elementi. Koda uspešno natisne zgoščeno vrednost tuple. Vendar, ko poskuša natisniti zgoščeno vrednost seznama, ki je spremenljiva, dvigne aTypeError>ker spremenljivih predmetov, kot so seznami, ni mogoče neposredno zgostiti.
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)))> |
>
>
Izhod:
The tuple hash value is : 8315274433719620810>
Izjeme:
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'> Primer 3: hash() za nespremenljivi tuple Object
Ta koda Python inicializira torko z imenomvar>ki vsebuje znake 'G', 'E', 'E' in 'K'. Thehash()>funkcija se uporablja za ustvarjanje zgoščene vrednosti za tuplevar>. V tem primeru koda natisne zgoščeno vrednost tuple, ki je edinstveno celo število, ki predstavlja tuple na podlagi njegove vsebine in strukture.
Python3
# hash() for immutable tuple object> var>=> (>'G'>,>'E'>,>'E'>,>'K'>)> print>(>hash>(var))> |
>
>Izhod
3843673861956289951>
Primer 4: hash() vklopljen njim uporabni predmet
metoda hash(), ki jo uporablja en nespremenljiv objekt, če to uporabimo na spremenljivem objektu, kot je seznam, nabor, slovarji, bo ustvarila napako.
Python3
koliko je star pete davidson
l>=> [>1>,>2>,>3>,>4>]> print>(>hash>(l))> |
>
>
Izhod
TypeError: unhashable type: 'list'>
Primer 5: hash() na objektu po meri
Tukaj bomo preglasili metode __hash()__ za klic hash(), metoda __eq__() pa bo preverila enakost obeh predmetov po meri.
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))> |
>
>Izhod
The hash is: 7627717261227283506 The hash is: 7627717261227283506>