1
我是python的新手,难以获取对象以便在Python中存储和访问数组或列表。从Python列表存储和检索对象
我试着做这样的事情:
class NodeInfo:
def __init__(self, left, value, right):
self.l = left
self.r = right
self.v = value
tree[0] = NodeInfo(0,1,2)
tree[0].l = 5
tree[0].r = 6
tree[0].v = 7
当我尝试将值赋给或尝试从变量看,我得到以下错误:
tree[0] = NodeInfo(0,1,2)
NameError: name 'tree' is not defined
上午什么我做错了,还是有不同的方式来分配和读取Python中的数组或列表中的对象。
不相关,但你也可能想放弃你的旧风格类,并获得一个新的风格类。也就是说,'class NodeInfo:'成为'class NodeInfo(object):'。除非你使用的是Python 3(当老式类被丢弃时它并不重要),但是我仍然更喜欢使用相同的约定。见[this](http://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python)。 –