Python 3.4 程序应该在 类 中使用 __slots__ 吗?
Should Python 3.4 programs use __slots__ in classes?
类 的 __slots__
属性是用 Python 2 或更早版本创建的,根据对 Python __slots__
的回答的评论,似乎 Python 3.3 已经改进,因此内存大小的优势可能不是在例如 Python 3.4 程序中使用 __slots__
的原因。
所以我应该清理我的代码并删除 类 中 __slots__
的使用,或者即使在 Python 中仍然有使用 __slots__
的一些充分理由3.4 个程序?
通过使用 __slots__
你告诉 Python 不要在你的实例上有 __dict__
属性。这是为了在您可能有成千上万个实例的情况下节省内存。这是在 Python 2 中使用它们的原因,也是在 Python 3.
中使用它们的原因
类 的 __slots__
属性是用 Python 2 或更早版本创建的,根据对 Python __slots__
的回答的评论,似乎 Python 3.3 已经改进,因此内存大小的优势可能不是在例如 Python 3.4 程序中使用 __slots__
的原因。
所以我应该清理我的代码并删除 类 中 __slots__
的使用,或者即使在 Python 中仍然有使用 __slots__
的一些充分理由3.4 个程序?
通过使用 __slots__
你告诉 Python 不要在你的实例上有 __dict__
属性。这是为了在您可能有成千上万个实例的情况下节省内存。这是在 Python 2 中使用它们的原因,也是在 Python 3.