Posted By

magicrebirth on 12/22/09


Tagged

lambda dictionary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

magicrebirth


Python: lambda in a dictionary


 / Published in: Python
 

  1. // add a function as an element of a dictionary
  2. >>> {1: lambda: len('ciao')}
  3. >>> {1: <function <lambda> at 0x101d530>}
  4. // get element with specified key
  5. >>> {1: lambda: len('ciao')}[1]
  6. >>> <function <lambda> at 0x101d5f0>
  7.  
  8. >>> a = {1: lambda: len('ciao')}[1]
  9. >>> a
  10. >>> <function <lambda> at 0x101d670>
  11. // run the function-element
  12. >>> a()
  13. >>> 4
  14.  
  15. // or run it directly from here!
  16. >>> {1: lambda: len('ciao')}[1]()
  17. >>> 4

Report this snippet  

You need to login to post a comment.