我最近一直在开发一个小项目,其中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并将旧的解压到其中似乎效率很低。我的问题是是否有;
maxlen创建后更改属性值的方法deque,或者如果这是不可能/不明智的;