gpt4 book ai didi

Python 属性错误 : class object has no attribute

转载 作者:行者123 更新时间:2023-12-05 09:21:24 24 4
gpt4 key购买 nike

当我尝试运行我正在编写的类的代码时,我得到一个 AttributeError 并且我不确定为什么。具体报错如下:

    self.marker = self.markers[marker[1:]]
AttributeError: 'TTYFigureData' object has no attribute 'markers'

这是我正在写的类(class)的一部分:

class TTYFigureData(object):
"""
data container of TTYFigure
"""
def __init__(
self,
x, # x values
y, # y values
marker = "_.", # datum marker
plot_slope = True
):
self.x = x
self.y = y
self.plot_slope = plot_slope
self.set_marker(marker)
self.markers = {
"-" : u"None" ,
"," : u"\u2219"
}

def set_marker(
self,
marker
):
if marker in [None, "None", u"None", ""]:
self.plot_slope = True
self.marker = ""
elif marker[0] == "_":
self.marker = self.markers[marker[1:]]
else:
self.marker = marker

我哪里错了?

最佳答案

在您的 __init__ 方法中,您调用 self.set_marker() 您设置 self.markers 之前:

self.set_marker(marker)
self.markers = {
"-" : u"None" ,
"," : u"\u2219"
}

所以当 set_marker() 运行时,还没有 self.markers 。将调用下移一行:

self.markers = {
"-" : u"None" ,
"," : u"\u2219"
}
self.set_marker(marker)

关于Python 属性错误 : class object has no attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com