title: Function that replaces itself with builtin after first execution
body: <p>Is it possible to write a function that replaces itself after its first execution?</p>
  
  <p><strong>first.py:</strong></p>
  
  <pre><code>from second import max
  x = max(1,2,3)
  y = max(4,5,6)
  </code></pre>
  
  <p><strong>second.py:</strong></p>
  
  <pre><code>def max(*args):
      ... #rewire references
      return min(args)
  </code></pre>
  
  <p>Can I make it so the second call to <code>max</code> calls the builtin, while the first one calls the one from <strong>second.py</strong>? I tried modifying the <code>globals()</code> dict, but that had no effect, of course, neither did declaring <code>global max</code> in the custom <code>max</code> function, even with it renamed (and later named max again).</p>
  
  <p>Is there any way this could be possible? Can I access the importing module in some way?</p>
  
  <p><em>(Before someone comes with the inevitable comment: I know this is a bad idea, I don't plan to use it in production, if it's possible. I'm just interested in knowing whether it is doable.)</em></p>
  
  <p><strong>edit:</strong> To clarify, afterwards this should be how it looks like in the REPL:</p>
  
  <pre><code>&gt;&gt;&gt; from second import *
  &gt;&gt;&gt; max
  &lt;function max at 0x10e501400&gt;
  &gt;&gt;&gt; x = max(1,2,3)
  &gt;&gt;&gt; max
  &lt;built-in function max&gt;
  </code></pre>