| thunk (2007-02-07) | index /home/ping/web/python/thunk.py |
Shorthand syntax for making thunks.
Use _ as a placeholder in an expression to create a one-argument
function that fills in the placeholder and evaluates the expression.
For example:
>>> from thunk import _
>>> numbers = [3, 8, 4, 1, 2]
>>> filter(_ < 5, numbers)
[3, 4, 1, 2]
>>> map(_ + 7, numbers)
[10, 15, 11, 8, 9]
>>> words = 'lovely spam and eggs'.split()
>>> filter(_.endswith_('s'), words)
['eggs']
>>>
Notice that when calling a method on _ you need to append an
underscore to the method name (in order to distinguish a method
call from an attribute lookup.
The illusion is far from complete, but it's a fun hack.
| Classes | ||||||||||||||
|
| ||||||||||||||
| Functions | ||
| ||
| Data | ||
| __author__ = 'Ka-Ping Yee' __date__ = '2007-02-07' | ||
| Author | ||
| Ka-Ping Yee | ||