attributeError:can can pickle local对象'< gt;。

发布于 2025-01-31 04:58:55 字数 693 浏览 4 评论 0原文

我正在尝试腌制一本使用以下方式创建的嵌套词典:

collections.defaultdict(lambda: collections.defaultdict(int))

我的简化代码是这样的:

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(lambda: collections.defaultdict(int))
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)

但是它给出了错误:

AttributeError: Can't pickle local object 'A.funA.<locals>.<lambda>'

在打印字典后,它显示出来:

defaultdict(<function A.funA.<locals>.<lambda> at 0x7fd569dd07b8> {...}

我尝试在该函数中将字典全局变成,但错误是相同的。 我感谢对此问题的任何解决方案或见解。谢谢!

I am trying to pickle a nested dictionary which is created using:

collections.defaultdict(lambda: collections.defaultdict(int))

My simplified code goes like this:

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(lambda: collections.defaultdict(int))
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)

However it gives error:

AttributeError: Can't pickle local object 'A.funA.<locals>.<lambda>'

After I print dictionary it shows:

defaultdict(<function A.funA.<locals>.<lambda> at 0x7fd569dd07b8> {...}

I try to make the dictionary global within that function but the error is the same.
I appreciate any solution or insight to this problem. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2025-02-07 04:58:55

Pickle记录对功能(模块和功能名称)的引用,而不是功能本身。当取消打印时,它将加载模块并按名称获取功能。 lambda创建没有名称的匿名函数对象,并且加载程序找不到。解决方案是切换到命名函数。

def create_int_defaultdict():
    return collections.defaultdict(int)

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(create_int_defaultdict)
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)

pickle records references to functions (module and function name), not the functions themselves. When unpickling, it will load the module and get the function by name. lambda creates anonymous function objects that don't have names and can't be found by the loader. The solution is to switch to a named function.

def create_int_defaultdict():
    return collections.defaultdict(int)

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(create_int_defaultdict)
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)
め七分饶幸 2025-02-07 04:58:55

正如@tdlaney所解释的那样,lambda创建一个无法腌制的匿名功能。最简洁的解决方案是将lambda替换为partial(无需新功能):

from functools import partial

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(partial(collections.defaultdict, int))
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)

As @tdlaney explained, lambda creates an anonymous function which can't be pickled. The most concise solution is to replace lambda with partial (no new function needed):

from functools import partial

class A:
  def funA(self):
    #create a dictionary and fill with values
    dictionary = collections.defaultdict(partial(collections.defaultdict, int))
    ...
    #then pickle to save it
    pickle.dump(dictionary, f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文