[Python-Dev] Shorthand for lambda

Jeremy Hylton jhylton at gmail.com
Wed Mar 23 17:44:10 CET 2005


For filter and map, list comprehensions and generator expressions are
the answer.

>>> numbers = [5, 9, 56, 34, 1, 24, 37, 89]
>>> [x for x in numbers if x < 30]
[5, 9, 1, 24]
>>> (x for x in numbers if x < 30)
<generator object at 0x00B1FCD8>
>>> list(_)
[5, 9, 1, 24]

Jeremy

On Wed, 23 Mar 2005 10:33:53 -0600 (CST), Ka-Ping Yee
<python-dev at zesty.ca> wrote:
> Hey folks,
> 
> I'm sitting over here in the AppleScript talk and Jacob is explaining a
> module called 'appscript' that interfaces to the Apple Events system.
> 
> What caught my eye was this example:
> 
>     from appscript import *
>     ab = app('Address Book')
>     people = ab.people.filter(its.emails != [])
> 
> That last line asks the Address Book to select only entries with
> e-mail addresses.  The weird 'its' object comes from the appscript
> module -- asking for its properties and using operators causes it
> to set up thunks for you.
> 
> It dawned on me that you could use this idea to make the whole
> filter/lambda experience vastly more pleasant.  I whipped up a quick
> implementation:
> 
>     >>> from placeholder import _
>     >>> numbers = [5, 9, 56, 34, 1, 24, 37, 89]
>     >>> filter(_ < 30, numbers)
>     [5, 9, 1, 24]
>     >>> map(_ + 10, numbers)
>     [15, 19, 66, 44, 11, 34, 47, 99]
>     >>>
> 
> Look ma, no lambdas!
> 
> I bet someone has already done this before, right?
> 
> -- ?!ng
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/jeremy%40alum.mit.edu
>


More information about the Python-Dev mailing list