Python AttributeError: class object has no attribute
Python AttributeError: class object has no attribute
当我尝试 运行 我正在编写的 class 的代码时,我得到了 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)
解释了问题并给出了最小的解决方案。但是,鉴于 self.markers
似乎是常量,我会将其设为 class 属性 而不是为每个实例重新创建它:
class TTYFigureData(object):
"""Data container of TTYFigure."""
MARKERS = {
"-": u"None" ,
",": u"\u2219",
}
def __init__(self, x, y, marker='_.', plot_slope=True):
"""Document parameters here as required."""
self.x = x
self.y = y
self.plot_slope = plot_slope
self.set_marker(marker)
def set_marker(self, marker):
"""See also here - usage guidance is also good."""
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
(注意也根据 the official guidance 更改样式)
当我尝试 运行 我正在编写的 class 的代码时,我得到了 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)
self.markers
似乎是常量,我会将其设为 class 属性 而不是为每个实例重新创建它:
class TTYFigureData(object):
"""Data container of TTYFigure."""
MARKERS = {
"-": u"None" ,
",": u"\u2219",
}
def __init__(self, x, y, marker='_.', plot_slope=True):
"""Document parameters here as required."""
self.x = x
self.y = y
self.plot_slope = plot_slope
self.set_marker(marker)
def set_marker(self, marker):
"""See also here - usage guidance is also good."""
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
(注意也根据 the official guidance 更改样式)