小编ben*_*ben的帖子

更改 collections.deque 的 maxlen 属性

我最近一直在开发一个小项目,其中maxlena 的属性collections.deque是根据用户可配置的参数设置的。状态在会话之间存储;任何活动都deque构成该状态的一部分。因此,如果会话之间存在配置更改,则需要更改maxlen每个恢复的属性。deque目前,我正在这样做:

if current_setting != last_setting:
    # Deques are in a dictionary
    for k in dictionary:
        # Other data types and structures exist in this dictionary
        if dictionary[k].__class__() == deque():
            dictionary[k] = deque([*dictionary[k]], maxlen=current_setting)
Run Code Online (Sandbox Code Playgroud)

创建一个新的deque并将旧的解压到其中似乎效率很低。我的问题是是否有;

  1. maxlen创建后更改属性值的方法deque,或者如果这是不可能/不明智的;
  2. 是否有更有效的方法来完成上面示例中实现的目标?

python python-3.x

5
推荐指数
1
解决办法
1623
查看次数

标签 统计

python ×1

python-3.x ×1