如何将属性保留在 __dict__ 之外?

发布于 2024-11-15 04:35:04 字数 374 浏览 2 评论 0原文

我正在创建一系列对象实例,我使用它们的 __dict__ 属性保存和加载它们的属性。我想将一些属性保留在 __dict__ 之外,只是因为我不希望保存或加载它们。

更具体地说:

我创建了一个仅包含子对象列表的父对象。子对象的 __dict__ 被保存到文件中并稍后加载以再次实例化它们。我希望子对象具有对父对象的引用,但我不希望将该引用保存到文件中,也不希望从文件中加载,因为它没有意义。

是否有任何语法可以从 __dict__ 中排除该属性?

PS:许多答案建议使用 pickle,我目前使用 json 来保持数据可读。 EM>

I am creating a series of object instances, the attributes of which i save and load using their __dict__ attribute. I would like to keep some attributes outside of __dict__, simply because i do not want them to be saved or loaded.

More specifically:

I created a parent object merely holding a list of children objects. Children objects' __dict__ is saved to file and loaded later on to instantiate them again. I want the children objects to have a reference to the parent, but i do not want that reference to be saved to, nor loaded from file, as it would not make sense.

Is there any syntax that would exclude the attribute from __dict__?

PS: many answers suggest using pickle, i am currently using json to keep data human readable.

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

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

发布评论

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

评论(4

深爱不及久伴 2024-11-22 04:35:04

为您不想保存的属性添加一些前缀,并在保存时排除它们。

Add some prefix for attributes you don't want to save and exclude them while saving.

傲世九天 2024-11-22 04:35:04

这种方法行不通,因此无法解决您的问题。相反,您应该扩展框架以允许您指定要从保存/加载中省略哪些属性。

然而,您没有使用已经提供此类功能的内置持久性技术之一(例如 pickle)似乎很奇怪。

This approach won't work and so is not going to solve your problem. You should instead extend your framework to allow you to specify which attributes were to be omitted from the save/load.

However, it seems odd that you aren't using one of the built in persistence techniques, e.g. pickle, that already offer such features.

高速公鹿 2024-11-22 04:35:04

如果您使用 python pickle 您可以创建两个方法 __getstate____setstate__ 允许您自定义属性像这样泡菜:

class Coordinate(object):
     def __init__(self, x, y, z=0):
         self.x, self.y, self.z = x, y, z

     def __getstate__(self):
        """By default if no __getstate__ was defined __dict__ will be pickle, but in
        this case i don't want to pickle self.z attribute because of that this
        method return a dict in the form {'x': ..., 'y': ...}.

        """   
        return {
            "x": self.x,
            "y": self.y,
        }

    def __setstate__(self, data):
        self.__init__(data["x"], data["y"])

If you are using python pickle you can create the two method __getstate__ and __setstate__ that will allow you to customize the attribute to pickle like so:

class Coordinate(object):
     def __init__(self, x, y, z=0):
         self.x, self.y, self.z = x, y, z

     def __getstate__(self):
        """By default if no __getstate__ was defined __dict__ will be pickle, but in
        this case i don't want to pickle self.z attribute because of that this
        method return a dict in the form {'x': ..., 'y': ...}.

        """   
        return {
            "x": self.x,
            "y": self.y,
        }

    def __setstate__(self, data):
        self.__init__(data["x"], data["y"])
陌生 2024-11-22 04:35:04

您可以重写对象的 __getattr____setattr__ 方法,以向 __dict__ 隐藏您的属性。但您需要将附加属性存储在其他地方。它还使得很难弄清楚你的对象真正拥有哪些成员。所以这更像是一种黑客行为,而不是一个好的解决方案。

You can override the __getattr__ and __setattr__ methods of your object to hide your attributes from __dict__. But you need to store your additional attributes somewhere else. Also it makes it very hard to figure out which members your object really has. So it is more a hack than a good solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文