title: How do I forward-declare a function to avoid `NameError`s for functions defined later?
body: <p>Is it possible to forward-declare a function in Python?  I want to sort a list using my own <code>cmp</code> function before it is declared.</p>
  <pre><code>print &quot;\n&quot;.join([str(bla) for bla in sorted(mylist, cmp = cmp_configs)])
  </code></pre>
  <p>I've put the definition of <code>cmp_configs</code> method after the invocation.  It fails with this error:</p>
  <pre><code>NameError: name 'cmp_configs' is not defined
  </code></pre>
  <p>Is there any way to &quot;declare&quot; <code>cmp_configs</code> method before it's used?</p>
  <p>Sometimes, it is difficult to reorganize code to avoid this problem.  For instance, when implementing some forms of recursion:</p>
  <pre><code>def spam():
      if end_condition():
          return end_result()
      else:
          return eggs()
  
  def eggs():
      if end_condition():
          return end_result()
      else:
          return spam()
  </code></pre>
  <p>Where <code>end_condition</code> and <code>end_result</code> have been previously defined.</p>
  <p>Is the only solution to reorganize the code and always put definitions before invocations?</p>