def is_hashable(obj):
s = set()
try:
s.add(obj)
except TypeError:
return False
else:
return True
a = lambda : 0
print(is_hashable(a))
# True
b = [0]
print(is_hashable(b))
# False
c = (0,)
print(is_hashable(c))
# True
d = {0}
print(is_hashable(d))
# False
e = frozenset(d)
print(is_hashable(e))
# True
f = {0: 0}
print(is_hashable(f))
# False
class Foo:
pass
g = Foo()
print(is_hashable(g))
# True
일반적인 해시맵에 대한 상식으로 풀면 틀림 ㅋㅋ
아 깜박하고 is_hashable을 set로 구현했는데 dict로 구현했을 때랑 완전히 동일함 ㅇㅇ
이거 통과하면 set의 아이템으로도, dict의 키로도 모두 사용 가능
댓글 0