serializing and deserializing lambdas

后端 未结 2 1355
栀梦
栀梦 2021-02-05 11:10

I would like to serialize on machine A and deserialize on machine B a python lambda. There are a couple of obvious problems with that:

  • the pickle module does not s
2条回答
  •  星月不相逢
    2021-02-05 12:10

    I'm not sure exactly what you want to do, but you could try dill. Dill can serialize and deserialize lambdas and I believe also works for lambdas inside closures. The pickle API is a subset of it's API. To use it, just "import dill as pickle" and go about your business pickling stuff.

    >>> import dill
    >>> testme = lambda x: lambda y:x
    >>> _testme = dill.loads(dill.dumps(testme))
    >>> testme
     at 0x1d92530>
    >>> _testme
     at 0x1d924f0>
    >>> 
    >>> def complicated(a,b):
    ...   def nested(x):
    ...     return testme(x)(a) * b
    ...   return nested
    ... 
    >>> _complicated = dill.loads(dill.dumps(complicated))
    >>> complicated 
    
    >>> _complicated
    
    

    Dill registers it's types into the pickle registry, so if you have some black box code that uses pickle and you can't really edit it, then just importing dill can magically make it work without monkeypatching the 3rd party code. Or, if you want the whole interpreter session sent over the wire as an "python image", dill can do that too.

    >>> # continuing from above
    >>> dill.dump_session('foobar.pkl')
    >>>
    >>> ^D
    dude@sakurai>$ python
    Python 2.7.5 (default, Sep 30 2013, 20:15:49) 
    [GCC 4.2.1 (Apple Inc. build 5566)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dill
    >>> dill.load_session('foobar.pkl')
    >>> testme(4)
     at 0x1d924b0>
    >>> testme(4)(5)
    4
    >>> dill.source.getsource(testme)
    'testme = lambda x: lambda y:x\n'
    

    You can easily send the image across ssh to another computer, and start where you left off there as long as there's version compatibility of pickle and the usual caveats about python changing and things being installed. As shown, you can also extract the source of the lambda that was defined in the previous session.

    Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.

提交回复
热议问题