title: How do I ensure a list of arbitrary function calls is not eagerly evaluated past the short circuit point while processing in Python?
body: <p>For example, given </p>
  
  <pre><code>def expensive_call(x):
      print(x)
      if x == "d":
          return x
  def expensive_call_2(x, y):
      print(x)
      print(y)
      return x + y
  
  a = [expensive_call("a"), expensive_call_2("b", "c"), expensive_call("d")]
  next((e for e in a if e is not None), 'All are Nones')
  </code></pre>
  
  <p>the output is</p>
  
  <pre><code>a
  b
  c
  d
  Out[22]: 'bc'
  </code></pre>
  
  <p>Since <code>expensive_call("d")</code> is eagerly evaluated, note that "d" is printed even though the <code>next</code> call short-circuits at the second call with an output of "bc".</p>
  
  <p>I am hard coding the calls in the list <code>a</code>, and <code>a</code> doesn't have to be a list data structure. </p>
  
  <p>One potential solution is as follows:</p>
  
  <pre><code>a = ['expensive_call("a")', 'expensive_call_2("b", "c")', 'expensive_call("d")']
  def generator():
      for e in a:
          r = eval(e)
          if r is not None:
              yield r
  next(generator(), 'All are Nones')
  </code></pre>
  
  <p>the output is</p>
  
  <pre><code>a
  b
  c
  Out[23]: 'bc'
  </code></pre>
  
  <p>as desired. However, I don't really like having to use eval. I would also prefer not to use any solution that initially keeps the function pointer and the arguments separate like <code>(expensive_call, ("a"))</code>. Ideally I would have something like</p>
  
  <pre><code>a = lazy_magic([expensive_call("a"), expensive_call_2("b", "c"), expensive_call("d")])
  next((e for e in a if e is not None), 'All are Nones')
  </code></pre>
  
  <p>Note that <a href="https://stackoverflow.com/a/3405828/2750819">https://stackoverflow.com/a/3405828/2750819</a> is a similar question but only applies for when the functions have the same method signature.</p>