Advertisement
aneroid

puritanical functional code to avoid deep nesting

Feb 25th, 2021
1,518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. # for https://chat.stackoverflow.com/transcript/message/51671357#51671357
  2.  
  3. def looper():
  4.     def lst1_func():
  5.         nonlocal i1, a, b, c, d
  6.         # do stuff
  7.    
  8.     def lst2_func():
  9.         nonlocal i2, a, b, c, d
  10.         # do stuff
  11.    
  12.     # def lst3_func, lst4_func here
  13.    
  14.     triplets = [(lst1, lst1_func, lambda x: x.upper() in bar),
  15.                 (lst2, lst2_func, lambda x: x in foo),
  16.                 (lst3, lst3_func, lambda x: x),
  17.                 (lst4, lst4_func, lambda x: x not in lst1),
  18.     ]
  19.    
  20.     for lst, func, condition in triplets:
  21.         for item in lst:
  22.             if condition(item):
  23.                 return
  24.             func()
  25.    
  26.     print('Never broke')
  27.  
  28.  
  29. looper()
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement