SHARE
TWEET

compact if/else for python

a guest Aug 23rd, 2010 203 Never
  1. def ifel(expression):
  2.     def ifcase(ifstatement):
  3.         if expression:
  4.             def ifevaluator(elsestatement):
  5.                 return ifstatement
  6.             return ifevaluator
  7.         else:
  8.             def elseevaluator(elsestatement):
  9.                 return elsestatement
  10.             return elseevaluator
  11.     return ifcase
  12.  
  13. print ">>> ifel(35*20 > 10)(100)(20)"
  14. print ifel(35*20 > 10)(100)(20)
  15. print ">>> ifel(35*20 < 10)(100)(20)"
  16. print ifel(35*20 < 10)(100)(20)
RAW Paste Data
Top