starlark
starlark copied to clipboard
Are functions hashable?
From @alandonovan's comment in a prior issue:
% starlark
Welcome to Starlark (go.starlark.net)
>>> {len: 1}
{<built-in function len>: 1}
>>> len in {}
False
>>> {}.get(len, False)
False
% python2
Python 2.7.17rc1 (default, Oct 10 2019, 10:26:01)
>>> {len: 1}
{<built-in function len>: 1}
>>> len in {}
False
>>> {}.get(len, False)
False
% python3.8
Python 3.8.1 (default, Dec 31 2019, 14:30:41)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> {len: 1}
{<built-in function len>: 1}
>>> len in {}
False
>>> {}.get(len, False)
False
% blaze-bin/third_party/bazel/src/main/java/com/google/devtools/starlark/Starlark
>> {len: 1}
unhashable type: 'function'
>> len in {}
unhashable type: 'function'
>> {}.get(len, False)
unhashable type: 'function'
Note that for Bazel, function hashability is also related to concerns around serializing and fingerprinting/digesting functions. The details of which are not all paged into my head right now...