-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.py
57 lines (39 loc) · 1.29 KB
/
hashing.py
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
53
54
55
56
57
"""
Using dunder methods we can sort the items from a directory list by name and type.
Course: Python objects with special methods - Create better classes!.
Section: 3 Equality Operators.
Chapter: 13. Hash.
* Note: The hash of an object must never change during its lifetime.
- Numbers will always contain the same hash.
- Strings will contain a different hash every single session
"""
#Show the hash behavior with numbers.
a = 10
print(hash(a))
b = 0.1
print(hash(b))
c = 347268.3465
print(hash(c))
#Show the hash behavior with strings.
s = "Hello"
print(hash(s))
print("\n\n\n -----------------------------------------------------\n\n\n")
class Employee:
def __init__(self, emp_id, name):
self.emp_id = emp_id
self.name = name
def __eq__(self, other):
return self.emp_id == other.emp_id
def __hash__(self):
return self.emp_id
"""
e1 is used as a key in the buddies dictionary.
When a class is used as a key and it define the __eq__ dunder method it must to have __hash__ method too.
In other wise python will trow an error.
buddies = {e1:e2}
TypeError: unhashable type: 'Employee'
"""
e1 = Employee(1, "Vera")
e2 = Employee(2, "Chuck")
buddies = {e1:e2}
print(buddies[e1].name)