You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拦截dataclass成员的.append调用并替换为PyROOT std vector的push_back

解决方案

核心思路是用代理类封装底层的PyROOT std::vector,完整模拟Python列表的接口,所有用户操作直接转发到vector实例,既不需要额外复制数据,也能保证底层格式符合ROOT TTree的写入要求。

方案1:基于抽象基类的极简实现

继承Python标准库的collections.abc.MutableSequence,只需实现5个基础方法,就能自动获得所有原生列表的常用功能(append、extend、pop、遍历等),无需逐个手动适配:

from collections.abc import MutableSequence
import ROOT

class RootVectorListProxy(MutableSequence):
    def __init__(self, root_vector):
        # 绑定底层ROOT vector,无数据复制
        self._root_vec = root_vector

    # 必须实现的5个基础方法
    def __getitem__(self, index):
        if isinstance(index, slice):
            return list(self._root_vec)[index]
        return self._root_vec[index]

    def __setitem__(self, index, value):
        self._root_vec[index] = value

    def __delitem__(self, index):
        del self._root_vec[index]

    def __len__(self):
        return self._root_vec.size()

    def insert(self, index, value):
        self._root_vec.insert(self._root_vec.begin() + index, value)

    # 优化打印效果,和原生列表表现一致
    def __repr__(self):
        return repr(list(self._root_vec))

方案2:dataclass 接入示例

将ROOT vector作为私有字段存储,通过property对外暴露代理实例,同时支持完整列表赋值和增量修改:

from dataclasses import dataclass, field

@dataclass
class YourDataClass:
    # 私有字段存储原生ROOT vector,直接用于后续TTree写入
    _int_arr: ROOT.std.vector[int] = field(default_factory=lambda: ROOT.std.vector[int]())

    @property
    def int_arr(self):
        # 每次访问返回代理实例,无额外内存开销
        return RootVectorListProxy(self._int_arr)

    @int_arr.setter
    def int_arr(self, value):
        # 支持用户直接传入列表/可迭代对象全量赋值
        self._int_arr.clear()
        for item in value:
            self._int_arr.push_back(item)

使用示例(用户侧完全无感知)

# 初始化
data = YourDataClass()
# 全量赋值
data.int_arr = [1,2,3]
# 增量操作,自动转发到底层vector的push_back
data.int_arr.append(4)
# 支持索引访问、长度查询等所有列表操作
print(data.int_arr[0]) # 输出1
print(len(data.int_arr)) # 输出4
# 写入TTree时直接取私有字段即可,无需转换
# tree.Fill() 直接传 data._int_arr 就行

方案优势

  • 零额外内存开销:全程只有一份ROOT vector数据,不存在同步问题
  • 接口完全兼容Python列表:用户不需要了解任何ROOT相关知识即可使用
  • 扩展性强:如果需要兼容列表的其他特殊方法,直接在代理类中添加对应实现即可

内容的提问来源于stack exchange,提问作者Lech Wiktor Piotrowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 05:54:02