Why is it that I can add normal callables and methods to a set, but not <some list>.append (for instance)?
For Example:
>>> l = []
>>> s = set()
>>> s.add(l.append)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> type(l.append)
<type 'builtin_function_or_method'>
>>> type(map)
<type 'builtin_function_or_method'>
>>> s.add(map)
>>> def func(): print 'func'
...
>>> s.add(func)
>>> print s
set([<built-in function map>, <function func at 0x10a659758>])
Edit: I noticed that l.append.__hash__() also gives this error
You cannot add
lists to a set because lists are mutable. Only immutable objects can be added to sets.l.appendis an instance method. You can think of it as if it were the tuple(l, list.append)— that is, it’s the list.append() method tied to the particular listl. The list.append() method is immutable butlis not. So while you can addlist.appendto the set you can’t addl.append.This works:
This does not work: