How to provide additional initialization for a subclass of namedtuple?(如何为 namedtuple 的子类提供额外的初始化?)
问题描述
假设我有一个像这样的 namedtuple:
Suppose I have a namedtuple like this:
EdgeBase = namedtuple("EdgeBase", "left, right")
我想为此实现一个自定义哈希函数,所以我创建了以下子类:
I want to implement a custom hash-function for this, so I create the following subclass:
class Edge(EdgeBase):
def __hash__(self):
return hash(self.left) * hash(self.right)
由于对象是不可变的,我希望哈希值只计算一次,所以我这样做:
Since the object is immutable, I want the hash-value to be calculated only once, so I do this:
class Edge(EdgeBase):
def __init__(self, left, right):
self._hash = hash(self.left) * hash(self.right)
def __hash__(self):
return self._hash
这似乎有效,但我真的不确定 Python 中的子类化和初始化,尤其是元组.这个解决方案有什么陷阱吗?有推荐的方法吗?好吗?提前致谢.
This appears to be working, but I am really not sure about subclassing and initialization in Python, especially with tuples. Are there any pitfalls to this solution? Is there a recommended way how to do this? Is it fine? Thanks in advance.
推荐答案
2017 年 结果表明 namedtuple 不是一个好主意.attrs 是现代替代方案.
edit for 2017: turns out namedtuple isn't a great idea. attrs is the modern alternative.
class Edge(EdgeBase):
def __new__(cls, left, right):
self = super(Edge, cls).__new__(cls, left, right)
self._hash = hash(self.left) * hash(self.right)
return self
def __hash__(self):
return self._hash
__new__ 是你想在这里调用的,因为元组是不可变的.不可变对象在 __new__ 中创建,然后返回给用户,而不是在 __init__ 中填充数据.
__new__ is what you want to call here because tuples are immutable. Immutable objects are created in __new__ and then returned to the user, instead of being populated with data in __init__.
cls 必须两次传递给 __new__ 上的 super 调用,因为 __new__ 对于历史/奇怪的原因隐含了一个 staticmethod.
cls has to be passed twice to the super call on __new__ because __new__ is, for historical/odd reasons implicitly a staticmethod.
这篇关于如何为 namedtuple 的子类提供额外的初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 namedtuple 的子类提供额外的初始化?
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
