Skip to content

Commit 91cbe60

Browse files
committed
py: Allow types to be hashable.
Quite natural to have d[int] = handle_int .
1 parent c6813d9 commit 91cbe60

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

py/obj.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
118118
return (machine_int_t)o_in;
119119
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_tuple)) {
120120
return mp_obj_tuple_hash(o_in);
121+
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_type)) {
122+
return (machine_int_t)o_in;
121123

122124
// TODO hash class and instances
123125
// TODO delegate to __hash__ method if it exists

tests/basics/types_hash.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Types are hashable
2+
print(hash(type) != 0)
3+
print(hash(int) != 0)
4+
print(hash(list) != 0)
5+
class Foo: pass
6+
print(hash(Foo) != 0)

0 commit comments

Comments
 (0)