Created
October 1, 2012 12:49
-
-
Save lambdaman2/3811602 to your computer and use it in GitHub Desktop.
Python: lambda in a dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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