lambdamusic
2/7/2013 - 9:27 PM

Python: Python: lambda in a dictionary

Python: 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