如何为 namedtuple 的子类提供额外的初始化?

How to provide additional initialization for a subclass of namedtuple?(如何为 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 的子类提供额外的初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)