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 8433465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:23:36+00:00 2026-06-10T06:23:36+00:00

Using the example def foo(a): def bar(b): return a+b return bar d = {1:foo(1),

  • 0

Using the example

def foo(a):
    def bar(b):
        return a+b
    return bar

d = {1:foo(1), 2:foo(2)}

It appears that pickle module will not work with a function not defined at the module scope, so pickling ‘d’ will not work. Is there another pickling mechanism available that I should consider?

  • 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-10T06:23:37+00:00Added an answer on June 10, 2026 at 6:23 am

    I’m afraid that you can’t pickle nested functions.

    The pickle module serializes functions by name. That is, if you have a function myfunc in a module mymodule it simply saves the name mymodule.myfunc and looks it up again when unserializing. (This is an important security and compatibility issue, as it guarantees that the unserializing code uses its own definition for the function, rather than the original definition which might be compromised or obsolete.)

    Alas, pickle can’t do that with nested functions, because there’s no way to directly address them by name. Your bar function, for instance, can’t be accessed from outside of foo.

    If you need a serializable object that works like a function, you can instead make a class with a __call__ method:

    class foo(object):
        def __init__(self, a):
            self.a = a
        def __call__(self, b): # the function formerly known as "bar"
            return self.a + b
    

    This works just like the nested functions in the question, and should pose no problem to pickle. Do be aware though, that you’ll need to have the same class definition available when you unserialize a foo instance.

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

Sidebar

Related Questions

I'm using the foo module which includes a few module level variables that dictate
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
I'm using the example found here: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01 The issue is that when I specify
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I have a problem using docstrings with decorators. Given the following example: def decorator(f):
Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
Have a look at the following Scala example: class A { def foo(x: Int):
I am using this simple example to understand Python's getattr function: In [25]: class
Taking a classic foo bar example: In models.py : Class Foo(models.Model): name = models.CharField(max_length=

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.