来自简单自制的 NameError class
NameError from simple self-made class
我创建了以下顶点 class:
"""VERTEX CLASS"""
class Vertex:
def __init__(self, name, total_weight, path_so_far):
self.name = name
self.total_weight = total_weight
self.path_so_far = path_so_far
def __str__(self):
return "(" + str(name) + "," + total_weight + "," + path_so_far + ")"
我正在尝试像这样制作一个 class 的对象:
t = Vertex(item[0],item[2],item[1])
当我编译我的程序时,我在堆栈上收到以下错误:
NameError: name 'name' is not defined
我已经检查了堆栈中的其他 NameError 条目,但到目前为止还没有看到与我的情况相关的任何修复。我错过了什么?
已在评论中解决:
Put a self. in front of all the field names in __str__. Like you did in __init__. – a_guest
我创建了以下顶点 class:
"""VERTEX CLASS"""
class Vertex:
def __init__(self, name, total_weight, path_so_far):
self.name = name
self.total_weight = total_weight
self.path_so_far = path_so_far
def __str__(self):
return "(" + str(name) + "," + total_weight + "," + path_so_far + ")"
我正在尝试像这样制作一个 class 的对象:
t = Vertex(item[0],item[2],item[1])
当我编译我的程序时,我在堆栈上收到以下错误:
NameError: name 'name' is not defined
我已经检查了堆栈中的其他 NameError 条目,但到目前为止还没有看到与我的情况相关的任何修复。我错过了什么?
已在评论中解决:
Put a self. in front of all the field names in __str__. Like you did in __init__. – a_guest