Unbound methods of types
Alex Martelli
aleaxit at yahoo.com
Mon Apr 30 03:29:26 EDT 2001
"Ben Hutchings" <ben.hutchings at roundpoint.com> wrote in message
news:u1yqb17us.fsf at roundpoint.com...
[snip]
> lists, there is no such module to help. To pick a better example, I
> can't say:
>
> map(types.DictType.keys, [{'a':1,'b':2,'c':3}, {'d':4,'e':5,'f':6}])
>
> and get back something like:
>
> [['b', 'c', 'a'], ['f', 'd', 'e']]
>
> and there is no module dict with a function keys that would help me
> here. Instead I have to use 'lambda d: d.keys()'. Actually, I could
> just use a list comprehension here; but if I wanted an unbound type
> method for some other purpose I'd have to resort to that lambda
I understand your issue, but, just to clarify things for other
readers: there is NO case that I know of, in Python, where one
HAS to use lambda; a named local/nested function will always do
at least as well, e.g.:
def keysOf(d): return d.keys()
map(keysOf, [{'a':1,'b':2,'c':3}, {'d':4,'e':5,'f':6}])
But that is by the by -- the only substantial difference between
such a named function and a lambda is, indeed, the name. It *would*
be interesting to be able to get an "unbound method" from a generic
type, or un-bind a builtin bound-method such as {}.keys (but, alas,
the __self__ of that object is read-only)...
Alex
More information about the Python-list
mailing list