老鱼 avatar

一文钱难倒英雄汉之【AttributeError】

chaimyu

Published: 23 Jul 2018 › Updated: 23 Jul 2018一文钱难倒英雄汉之【AttributeError】

一文钱难倒英雄汉之【AttributeError】

在工作中我们经常会碰到一些小问题,这些小问题在解决后来看都很简单容易,但是如果不知道解决方案可能得花费一天甚至几天的时间,解决过程经常让人抓狂,试图借“一文钱难倒英雄汉”这个标题来记录下这些问题和解决方案。

文章尽量短小精悍,不求甚解,看了能解决就好。

AttributeError: can't set attribute

最近改python-utransnet库,以前正常运行的突然出现以下错误:

class TransnetNodeRPC(GrapheneWebsocketRPC):

    def __init__(self, *args, **kwargs):
        super(TransnetNodeRPC, self).__init__(*args, **kwargs)
        self.chain_params = self.get_network()

在赋值self.chain_params时出现以下错误:

AttributeError: can't set attribute

解决办法

开始百思不得解,google了一下都是说property的问题,可是也没用property啊,而且在某些机器上能用,某些机器上不能用,仔细查了半天才考虑可能是基类GrapheneWebsocketRPC的问题,再找到基类库代码一看用到 getattr 了,那八成是这引起的,改成如下方式即可。

    @property
    def chain_params(self):
        return self._chain_params

    @chain_params.setter
    def chain_params(self, val):
        self._chain_params = val

Leave 一文钱难倒英雄汉之【AttributeError】 to:

Written by

One day sitting in the well looked at the sky, blowing the wind uphole, jumping out of the well, dancing in the wind!

Read more #python posts


Best Posts From 老鱼

We have not curated any of chaimyu's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From 老鱼