On Python lambdas and bytecode

From Roberto De Almeida at

Thoughts on sending a lambda to be executed remotely
import marshal
import types

x = 42
f = lambda y: x+y

code_source = marshal.dumps(f.func_code)
scope = f.func_globals

code = marshal.loads(code_source)
g = types.FunctionType(code, scope, "foo")

print f(1)
del f
del x
print g(1)

Comment on Twitter or Facebook.