python - will lambda func be 'cached'/remembered -


this question has answer here:

map( lambda x: len(x), alotofdata)

i writing code similar above, slower if put in lambda def in same line? or should assign to variable f avoid evolve lambda def in every iteration or python smart enough cache it?

the lambda evaluated once(converted code object), there's no need of of assigning variable.

>>> import dis >>> def func(): ...    map( lambda x: len(x), alotofdata)  ...     >>> dis.dis(func)   2           0 load_global              0 (map)               3 load_const               1 (<code object <lambda> @ 0x31c3d30, file "<ipython-input-30-27b0b12b0965>", line 2>)               6 make_function            0               9 load_global              1 (alotofdata)              12 call_function            2              15 pop_top                           16 load_const               0 (none)              19 return_value   

but map slow lambda's should use list comprehension or generator expression(if want iterator) here.

[len(x) x in alotofdata] 

or define full function, more readable:

def my_func(x):    #do x    return  [my_func(x) x in alotofdata] 

which more preferable use in python: lambda functions or nested functions ('def')?


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -