Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

An improvement on the switch-using-dictionaries example: instead of doing this...

  keycode = 2
  functions = {1: key_1_pressed, 2: key_2_pressed, 3: key_3_pressed}
  if functions.has_key(keycode):
      functions[keycode]()
  else:
      unknown_key_pressed()
you can save three lines by replacing the if-else block with:

  functions.get(keycode, unknown_key_pressed)()


Wow, yeah, that one is fairly obvious huh...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: