我有两个文件..
data.py
a1 = {"city":"Boston","code":"BOS"}
a2 = {"city":"Atlanta","code":"ATL"}
a3 = {"city":"Chicago","code":"ORD"}
a4 = ...
a5 = ...
a6 = ...
run.py
from data import *
Class Port_code():
def _init_()
...
def get_code():
x = globals().items()
for i in range(len(x)):
if x[i][1]['city'] == user_input_city:
print x[i][1]['code']
当我运行时,我得到以下错误:
typeerror:' module'对象没有属性' __ getitem __'
是因为我在班级里面使用它吗?
我有一个示例代码,它给了我我想要的东西不在课堂内。 sample.py
from data import *
user_input=raw_input("enter a City: ")
x = globals().items()
for i in range(len(x)):
if x[i][1]['city'] == user_input:
print x[i][1]['city'],
print " <== "+str(i)+" ==> ",
print x[i][1]['code']
** Sample.py工作正常! **
那么我可以清楚地了解在Class内使用吗?