本文介绍了如何为namedtuple的子类提供其他初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的namedtuple:

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的子类提供其他初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 07:57