mat*_*ieu 16 python lambda pickle
我想序列化机器A并在机器B上反序列化python lambda.这有几个明显的问题:
因此,我的问题:
Dav*_*ver 19
令人惊讶的是,检查lambda是否在没有相关闭合的情况下工作实际上相当容易.根据数据模型文档,您只需检查func_closure属性:
>>> def get_lambdas(): ... bar = 42 ... return (lambda: 1, lambda: bar) ... >>> no_vars, vars = get_lambdas() >>> print no_vars.func_closure None >>> print vars.func_closure (<cell at 0x1020d3d70: int object at 0x7fc150413708>,) >>> print vars.func_closure[0].cell_contents 42 >>>
然后序列化+加载lambda非常简单:
>>> import marshal, types >>> old = lambda: 42 >>> old_code_serialized = marshal.dumps(old.func_code) >>> new_code = marshal.loads(old_code_serialized) >>> new = types.FunctionType(new_code, globals()) >>> new() 42
值得一看的文档FunctionType:
function(code, globals[, name[, argdefs[, closure]]]) Create a function object from a code object and a dictionary. The optional name string overrides the name from the code object. The optional argdefs tuple specifies the default argument values. The optional closure tuple supplies the bindings for free variables.
请注意,您还可以提供闭包...这意味着您甚至可以序列化旧函数的闭包,然后在另一端加载它:)