2012-07-22 76 views
1

下面的代码返回一个错误KeyError: 500误差Python代码

def my_func(self, limit, list_type, **args): 
    type2abbr = {"allcategories": "ac", "categorymembers":"cm"} 
    abbr = type2abbr[list_type] 
    yield abbr 
if __name__ == "__main__": 
    abbr = my_func(500, "categorymembers") 
    print abbr 

输出:

 
    Traceback (most recent call last): 
     File "dater.py", line 72, in 
     bot.start() 
     File "dater.py", line 56, in start 
     for title, text in self.pages: 
     File "dater.py", line 25, in page_generator 
     for item in self.cats: 
     File "/home/ceradon/api.py", line 305, in list 
     abbr = type2abbr[list_type] 
    KeyError: 500 

谁能帮我想出解决办法吗?

+1

如果您运行该代码,它将不会执行任何操作,因为您从未在您发布的代码中调用该函数。您需要提供更多关于如何调用它的信息(例如,您传递了哪些参数)。 – BrenBarn 2012-07-22 03:10:18

+0

你可以把你的方法调用看起来像吗.. – jessh 2012-07-22 03:10:24

+2

没有更多的代码,这将是很难完全诊断。它看起来像'list_type'是500.你必须帮助我们理解为什么。 – 2012-07-22 03:10:30

回答

5

您已经实施了my_func,就好像它是一种方法(即假设为self),但您并未在任何对象上调用它。

要么你应该有一个对象,例如x.my_func(...),或者您应该从参数列表中删除self,