1

The following code return an error 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

Output:

    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

Can anyone help me figure this out please?

5
  • 1
    If you run that code it will do nothing, because you never call the function in the code you posted. You need to provide more info about how you are calling it (e.g., what arguments you pass). Commented Jul 22, 2012 at 3:10
  • Can you put in what your method call looks like.. Commented Jul 22, 2012 at 3:10
  • 2
    Without more code, it would be hard to fully diagnose. It looks like list_type is 500. You'd have to help us understand why. Commented Jul 22, 2012 at 3:10
  • 2
    Also, it seems strange to have a function with no loop using a yield statement. Are you sure you want yield and not simply return? Commented Jul 22, 2012 at 3:14
  • 1
    Since it doesn't look like you're using it inside of an object, is there any reason to include self? Commented Jul 22, 2012 at 3:20

1 Answer 1

5

You've implemented my_func as if it were a method (i.e. assuming self) but you are not calling it on any object.

Either you should have an object, e.g. x.my_func(...), or you should remove self, from the argument list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.