Movatterモバイル変換
[0]ホーム
Unbound methods of types
Alex Martellialeaxit at yahoo.com
Mon Apr 30 03:29:26 EDT 2001
"Ben Hutchings" <ben.hutchings at roundpoint.com> wrote in messagenews: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 lambdaI understand your issue, but, just to clarify things for otherreaders: there is NO case that I know of, in Python, where oneHAS to use lambda; a named local/nested function will always doat 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 betweensuch a named function and a lambda is, indeed, the name. It *would*be interesting to be able to get an "unbound method" from a generictype, 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-listmailing list
[8]ページ先頭