Lambda Functions (fwd)

Tim Peters tim_one at email.msn.com
Wed Aug 11 05:23:59 CEST 1999


> ...
> Python allows the use of lambda functions, small anonymous functions
> that can speed up processing because they are effectively inlined,

lambdas don't speed up anything in Python; they're not inlined (let alone
effectively <wink>); they're exactly as expensive as "def" functions to
invoke; they're often a little more expensive than "def" functions in
practice because of repeated construction, e.g.

def f(x):
    return x+1

for i in range(1000):
    print f(i)

constructs the function object f only once, while

for i in range(1000):
    print (lambda x: x+1)(i)

constructs an anonymous function object 1000 times.

next-thing-you-know-you'll-wake-up-in-a-tub-of-ice-with-"call-911"-
    scratched-into-your-chest-ly y'rs  - tim






More information about the Python-list mailing list