【发布时间】:2017-05-05 01:07:48
【问题描述】:
所以我有这段代码不起作用:
class Airport():
def _init_ (self):
self.Code_Icao=""
self.Latitude=""
self.Longitude=""
self.Schengen=False
A=Airport()
A.Code_Icao="LMAO"
A.Latitude="12321412"
A.Longitude="12123123"
B=Airport()
B.Code_Icao="JEJE"
B.Latitude="1231231"
B.Longitude="123123"
v=[]
v.append(A.__dict__)
v.append(B.__dict__)
print v[0].Code_Icao
我不知道为什么它不允许我访问列表组件的代码。
我不断收到此错误:
Traceback(最近一次调用最后一次):文件 “/Users/marc/PycharmProjects/untitled5/read.py”,第 18 行,在 print v[0].Code_Icao AttributeError: 'dict' 对象没有属性 'Code_Icao'
【问题讨论】:
-
v[0]是dict,而不是Airport。改用print v[0]['Code_Icao'] -
成功了!非常感谢你!
-
我认为 v = [A, B] 会更接近您的预期。