python-3.x - Python set update() 是否替换现有项目?
问题描述
如果我使用my_set.update(my_list)
where some elements my_list
are ==
existing elements in my_set
,它是否会替换现有项目?
解决方案
它没有:
from dataclasses import dataclass
@dataclass(frozen=True)
class MyType:
hashed: int
not_hashed: str
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
return hash((self.hashed, ))
x = {MyType(hashed=1, not_hashed="original")}
x.update([MyType(1, "new")])
print(x)
# {MyType(hashed=1, not_hashed='original')}
推荐阅读
- python - 将 Jupyter 中的“DataFrame.style”表保存为 png?
- jquery - 在下拉更改时显示 Bootstrap 模态表单,并在插入数据库后选择该值
- c - 有人可以帮我解决这个问题吗?(我是编程新手)
- google-analytics - 如何跟踪来自 url1->url2->url3 的用户数量?
- laravel - Laravel pluck 没有返回排序数组作为数据库
- excel - 如何为特定工作表运行此宏,工作簿有很多工作表?
- html - 如何通过自定义 CSS 为移动设备制作粘性标题
- visual-studio - 如何使用 Visual Studio 扩展处理深色主题?
- python - 空 SPARQL 查询结果
- ios - 暗/亮模式更改时可以重新加载应用程序吗?