파이썬[Python]: 내장함수 - hash 함수

내장함수 - hash 함수(function)


/// 설명

해시값을 반환하여줍니다.(해시값은 정수입니다.) - 같은 입력값에 매번 해시값이 변하는 의문점

※ 형식
hash(object)

reference
https://docs.python.org/3/library/functions.html

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
print(hash(True))  # 1
print(hash(...))  # -9223363241802529904
print(hash(NotImplemented))  # -9223363241802530356
print(hash(1))  # 1
print(hash(1.1))  # 230584300921369601
print(hash(complex(11)))  # 1000004
 
# -------------------------------------------
print(hash('string'))  #  every time, changed
 
test_tuple = (12345)
print(hash(test_tuple))  #  every time, changed
 
test_set = {'a''b''c''d''e'}
test_frozenset = frozenset(test_set)
print(hash(test_frozenset))  #  every time, changed
 
class A:
    def __init__(self, a):
        self.a = a
 
test_list = [12345]
test = A(test_list)
print(hash(test))  # every time, changed
 
 
# --- immutable objects ---
# hash value is unchanged: bool, constant, integer, float, complex
#
# every time, hash value is changed: string, tuple, frozenset, user-defined class
#    ---> Why?
#
# ex1)  input ---> 'name', hash('password')
#       Database   'LLL',  '6663412053233500253'
#
#       log in the database
#
#       typed 'LLL', 'password', if hash value is different
#       --->  'LLL', hash('password')=-5659871693760987716
#
#       can not log in...
#
# ex2)
# alice: 'string'                   ---> send: 'string'   --->  bob: 'string'
#        hash('string')             ---> with: hash value --->       hash('string')
#        hash value: 6663412053233500253             !=              hash value: -5659871693760987716
#
#        can not check...
#
# A cryptographic hash function must be deterministic, meaning that the same message always results in the same hash
# see: https://en.wikipedia.org/wiki/Cryptographic_hash_function
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

C 언어: sin 함수, cos 함수, tan 함수

파이썬[Python]: 내장함수 - bit_length 메서드