【问题标题】:Trying to call method on dict, getting AttributeError: 'dict' object attribute 'update' is read-only试图在 dict 上调用方法,得到 AttributeError: 'dict' object attribute 'update' is read-only
【发布时间】:2018-10-04 02:48:07
【问题描述】:

我不知道出了什么问题或导致错误的原因:

AttributeError: 'dict' object attribute 'update' is read-only

关于以下代码:

map = []
point1back = {}
point1fwd = {}
point1back.update = {'nextHop':point1Fwd, 'direction':1, 'distance':0}
point1fwd.update = {'nextHop':point1Fwd, 'direction':3, 'distance':160}
map.append(point1back)
map.append(point1fwd)

【问题讨论】:

  • p.update = args 不是你调用对象方法的方式,你想要的是p.update(args)。该代码尝试重新分配/覆盖字典的内置 update() 方法,这绝对不是您打算做的。

标签: python dictionary attributeerror


【解决方案1】:

dict.update 是一种方法,而不是您可以为其赋值的变量。试试这个:

point1back.update({'nextHop':point1Fwd, 'direction':1, 'distance':0})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-08
    • 2018-08-26
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2017-06-11
    相关资源
    最近更新 更多