Easy questions from a python beginner
Dave Angel
davea at ieee.org
Fri Jul 23 07:44:03 EDT 2010
wheres pythonmonks wrote:
> Funny... just spent some time with timeit:
>
> I wonder why I am passing in strings if the callback overhead is so light...
>
> More funny: it looks like inline (not passed in) lambdas can cause
> python to be more efficient!
>
>>>> import random
>>>> d = (['A','B'][random.randint(0,1)],x,random.gauss(0,1)) for x in xrange(0,1000000) ]
>>>> def A1(): j = lambda t: (t[2]*t[1],t[2]**2+5) for t in d ]
>>>>
>
>
>>>> def A2(): j = (t[2]*t[1],t[2]**2+5) for t in d ]
>>>>
>
>
But A1() gives a different result. It builds a list of function
objects. It doesn't actually do any of those multiplies. In fact, I
don't even think it would get the same answers if you then looped
through it, calling the functions it stored.
DaveA
More information about the Python-list
mailing list