Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:25:52+00:00 2026-06-09T07:25:52+00:00

I would like to serialize on machine A and deserialize on machine B a

  • 0

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 serialize or deserialize code. It only serializes the names of classes/methods/functions
  • some of the answers I found with google suggest the use of the low-level marshal module to serialize the func_code attribute of the lambda but they fail to describe how one could reconstruct a function object from the deserialized code object
  • marhshal(l.func_code) will not serialize the closure associated with the lambda which leads to the problem of detecting when a given lambda really needs a closure and warning the user that he is trying to serialize a lambda that uses a closure

Hence, my question(s):

  • how would one reconstruct a function from the deserialized (demarshaled) code object ?
  • how would one detect that a given lambda will not work properly without the associated closure ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:25:54+00:00Added an answer on June 9, 2026 at 7:25 am

    Surprisingly, checking whether a lambda will work without its associated closure is actually fairly easy. According to the data model documentation, you can just check the func_closure attribute:

    >>> 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
    >>>
    

    Then serializing + loading the lambda is fairly straight forward:

    >>> 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
    

    It’s worth taking a look at the documentation for the 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.
    

    Notice that you can also supply a closure… Which means you might even be able to serialize the old function’s closure then load it at the other end 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary in Python that I would like to serialize in JSON
I have three jobs that I would like to serialize in Jenkins. They should
I would like to XML serialize an object that has (among other) a property
I would like to serialize a boost::array, containing something that is already serializable. If
I have a complex Clojure data structure that I would like to serialize -
I have an C# class that I would like to serialize using XMLSerializer. But
I need to serialize a collection, but I would like to know if there
I have a model that I would like to serialize to an xml with
I would like to serialize and deserialize objects without having to worry about the
I would like to serialize the properties of the HttpBrowserCapibilities object so that it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.