如何为 namedtuple 的子类提供额外的初始化? [英] How to provide additional initialization for a subclass of namedtuple?

查看:21
本文介绍了如何为 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 的子类提供额外的初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆