Skip to content

Instantly share code, notes, and snippets.

@lambdaman2
Created October 1, 2012 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdaman2/3811602 to your computer and use it in GitHub Desktop.
Save lambdaman2/3811602 to your computer and use it in GitHub Desktop.
Python: lambda in a dictionary
// add a function as an element of a dictionary
>>> {1: lambda: len('ciao')}
>>> {1: <function <lambda> at 0x101d530>}
// get element with specified key
>>> {1: lambda: len('ciao')}[1]
>>> <function <lambda> at 0x101d5f0>
>>> a = {1: lambda: len('ciao')}[1]
>>> a
>>> <function <lambda> at 0x101d670>
// run the function-element
>>> a()
>>> 4
// or run it directly from here!
>>> {1: lambda: len('ciao')}[1]()
>>> 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment