来自派生类实例的泡菜基类

发布于 2025-02-08 22:41:26 字数 693 浏览 3 评论 0原文

我的基类看起来像:

@dataclass
class A:
    var1: List[float]
    var2: float

派生的类看起来像:

class B(A):
    def __init__(self, v1):
        super().__init__(v1, 0)

    def process():
        pass

我想要一种泡菜b实例的方法,以便仅需要类a才能取消它。我知道Python不支持升级,但是有办法实现我想要的东西吗?我想要以下内容:

l1 = [1., 2., 4.]
obj = B(l1)
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps((A)obj))

后续问题。这样的事情有效,但我不确定后果:

l1 = [1., 2., 4.]
obj = B(l1)
obj.__class__ = A
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps(obj))

任何帮助都将不胜感激。谢谢!

My base class looks like :

@dataclass
class A:
    var1: List[float]
    var2: float

and derived class looks like :

class B(A):
    def __init__(self, v1):
        super().__init__(v1, 0)

    def process():
        pass

I want a way to pickle an instance of B in a way such that only class A is required to unpickle it. I understand that python does not support upcasting but is there a way to achieve what I want? I want something like the following:

l1 = [1., 2., 4.]
obj = B(l1)
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps((A)obj))

Followup question. something like this works but I am not sure of the consequences:

l1 = [1., 2., 4.]
obj = B(l1)
obj.__class__ = A
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps(obj))

Any help would be appreciated. Thanks!

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

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

发布评论

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

评论(1

森末i 2025-02-15 22:41:26

您可以使用Module-level dataclasses.asdict.asdict.asdict.asdict> ()实用程序功能将b dataclass对象转换为dict(这是默认的出厂函数),然后使用它来创建一个base类实例。这与“ upcasting” b实例中的“ upcasting”与a实例不完全相同,但效果相似。

import dataclasses
from dataclasses import dataclass
import pickle
from typing import List


@dataclass
class A:
    var1: List[float]
    var2: float

class B(A):
    def __init__(self, v1):
        super().__init__(v1, 0)

    def process(self):
        pass

    def as_A(self):
        d = dataclasses.asdict(obj)
        return A(**d)


filename = 'derived_class.pkl'
l1 = [1., 2., 4.]
obj = B(l1)
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps(obj.as_A()))

You can effectively do what you want by using the module-level dataclasses.asdict() utility function to convert the B dataclass object into a dict (which is the default factory function), and then use that to create a base class instance. This isn't quite the same thing as "upcasting" the B instance into an A instance, but effect is similar.

import dataclasses
from dataclasses import dataclass
import pickle
from typing import List


@dataclass
class A:
    var1: List[float]
    var2: float

class B(A):
    def __init__(self, v1):
        super().__init__(v1, 0)

    def process(self):
        pass

    def as_A(self):
        d = dataclasses.asdict(obj)
        return A(**d)


filename = 'derived_class.pkl'
l1 = [1., 2., 4.]
obj = B(l1)
with open(filename, 'wb') as pickleFile:
    pickleFile.write(pickle.dumps(obj.as_A()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文