I recently tried the following commands in Python:
>>> {lambda x: 1: 'a'}
{<function __main__.<lambda>>: 'a'}
>>> def p(x): return 1
>>> {p: 'a'}
{<function __main__.p>: 'a'}
The success of both dict creations indicates that both lambda and regular functions are hashable. (Something like {[]: 'a'} fails with TypeError: unhashable type: 'list').
The hash is apparently not necessarily the ID of the function:
>>> m = lambda x: 1
>>> id(m)
140643045241584
>>> hash(m)
8790190327599
>>> m.__hash__()
8790190327599
The last command shows that the __hash__ method is explicitly defined for lambdas, i.e., this is not some automagical thing Python computes based on the type.
What is the motivation behind making functions hashable? For a bonus, what is the hash of a function?
__hash__function, so they clearly saw benefit in it. I would like to know what made them not just leave it alone.C, an object's type is represented by astruct PyTypeObject, and that struct'stp_hashmember defines what happens for__hash__(). If that member is initialized to 0, it inherits__hash__(). If you want the type to refuse to hash, you just initialize it toPyObject_HashNotImplementedinstead. (And if the type wants to implement a custom hash, you plug in the address of the type's custom hash function.) So maintenance burden really has nothing to do with it.