<i id='xxzCL'><tr id='xxzCL'><dt id='xxzCL'><q id='xxzCL'><span id='xxzCL'><b id='xxzCL'><form id='xxzCL'><ins id='xxzCL'></ins><ul id='xxzCL'></ul><sub id='xxzCL'></sub></form><legend id='xxzCL'></legend><bdo id='xxzCL'><pre id='xxzCL'><center id='xxzCL'></center></pre></bdo></b><th id='xxzCL'></th></span></q></dt></tr></i><div id='xxzCL'><tfoot id='xxzCL'></tfoot><dl id='xxzCL'><fieldset id='xxzCL'></fieldset></dl></div>

    <tfoot id='xxzCL'></tfoot>

    • <bdo id='xxzCL'></bdo><ul id='xxzCL'></ul>

        <small id='xxzCL'></small><noframes id='xxzCL'>

      1. <legend id='xxzCL'><style id='xxzCL'><dir id='xxzCL'><q id='xxzCL'></q></dir></style></legend>
      2. Python - 如何使这个不可腌制的对象可腌制?

        时间:2023-10-19
        • <bdo id='0WFGT'></bdo><ul id='0WFGT'></ul>
            <tbody id='0WFGT'></tbody>

              <tfoot id='0WFGT'></tfoot>

                <i id='0WFGT'><tr id='0WFGT'><dt id='0WFGT'><q id='0WFGT'><span id='0WFGT'><b id='0WFGT'><form id='0WFGT'><ins id='0WFGT'></ins><ul id='0WFGT'></ul><sub id='0WFGT'></sub></form><legend id='0WFGT'></legend><bdo id='0WFGT'><pre id='0WFGT'><center id='0WFGT'></center></pre></bdo></b><th id='0WFGT'></th></span></q></dt></tr></i><div id='0WFGT'><tfoot id='0WFGT'></tfoot><dl id='0WFGT'><fieldset id='0WFGT'></fieldset></dl></div>

                <legend id='0WFGT'><style id='0WFGT'><dir id='0WFGT'><q id='0WFGT'></q></dir></style></legend>

                <small id='0WFGT'></small><noframes id='0WFGT'>

                • 本文介绍了Python - 如何使这个不可腌制的对象可腌制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以,我有一个对象,里面有很多不可腌制的东西(pygame 事件、orderedDicts、时钟等),我需要将它保存到磁盘.

                  问题是,如果我可以让这个东西存储一个有进度的字符串(我只需要一个整数),那么我可以将它传递给对象的 init,它会重建所有这些东西.不幸的是,我使用的框架 (Renpy) 会腌制对象并尝试加载它,尽管我可以将其保存为单个整数,但我无法更改.p>

                  所以,我要问的是,我怎样才能覆盖方法,以便当 pickle 尝试保存对象时,它只保存进度值,并且每当它尝试加载对象时,它都会从进度值?

                  我已经看到一些关于 __repr__ 方法的讨论,但我不确定在我的情况下如何使用它.

                  解决方案

                  你要找的钩子是 __reduce__.它应该返回一个 (callable, args) 元组;callableargs 将被序列化,并且在反序列化时,将通过 callable(*args) 重新创建对象.如果您的类的构造函数采用 int,您可以将 __reduce__ 实现为

                  类 ComplicatedThing:def __reduce__(self):返回 (ComplicatedThing, (self.progress_int,))

                  您可以在元组中添加一些可选的额外内容,当您的对象图具有循环依赖关系时最有用,但您在这里不需要它们.

                  So, I have an object that has quite a bit of non-pickleable things in it (pygame events, orderedDicts, clock, etc.) and I need to save it to disk.

                  Thing is, if I can just get this thing to store a string that has the progress (a single integer is all I need), then I can pass it to the object's init and it will rebuild all of those things. Unfortunately, a framework I am using (Renpy) will pickle the object and attempt to load it, despite the fact that I could save it as a single integer, and I can't change that.

                  So, what I'm asking is, how can I override methods so that whenever pickle tries to save the object, it saves only the progress value, and whenever it tries to load the object, it creates a new instance from the progress value?

                  I've seen a bit talking bout the __repr__ method, but I am unsure how I would use this in my situation.

                  解决方案

                  The hook you're looking for is __reduce__. It should return a (callable, args) tuple; the callable and args will be serialized, and on deserialization, the object will be recreated through callable(*args). If your class's constructor takes an int, you can implement __reduce__ as

                  class ComplicatedThing:
                      def __reduce__(self):
                          return (ComplicatedThing, (self.progress_int,))
                  

                  There are a few optional extra things you can put into the tuple, mostly useful for when your object graph has cyclic dependencies, but you shouldn't need them here.

                  这篇关于Python - 如何使这个不可腌制的对象可腌制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何用 Python 保存数据? 下一篇:将 for 循环的输出保存到文件

                  相关文章

                • <legend id='k8MdI'><style id='k8MdI'><dir id='k8MdI'><q id='k8MdI'></q></dir></style></legend>
                    <tfoot id='k8MdI'></tfoot>
                  1. <small id='k8MdI'></small><noframes id='k8MdI'>

                      <bdo id='k8MdI'></bdo><ul id='k8MdI'></ul>
                  2. <i id='k8MdI'><tr id='k8MdI'><dt id='k8MdI'><q id='k8MdI'><span id='k8MdI'><b id='k8MdI'><form id='k8MdI'><ins id='k8MdI'></ins><ul id='k8MdI'></ul><sub id='k8MdI'></sub></form><legend id='k8MdI'></legend><bdo id='k8MdI'><pre id='k8MdI'><center id='k8MdI'></center></pre></bdo></b><th id='k8MdI'></th></span></q></dt></tr></i><div id='k8MdI'><tfoot id='k8MdI'></tfoot><dl id='k8MdI'><fieldset id='k8MdI'></fieldset></dl></div>