python代码出错

时间:2012-07-22 03:08:52

标签: python

以下代码返回错误KeyError: 500

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 个答案:

答案 0 :(得分:5)

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

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