title: In Python change a function name where function is dynamically created by types.FunctionType
body: <p>I could dynamically create a function and ran it without a problem, but somehow the new function kept the original name.</p>
  <p>This is my test code, test_copy_func2.py</p>
  <pre><code>#!/usr/bin/env python
  
  import types
  from typing import Callable
  
  def copy_func(f: Callable, name=None):
      fn = types.FunctionType(f.__code__, f.__globals__, 
                              name or f.__name__,f.__defaults__, f.__closure__)
      fn.__dict__.update(f.__dict__)
      return fn
  
  def f1():
      pass
  
  f2 = copy_func(f1, name=&quot;f2&quot;)
  
  print(f&quot;f1={f1}&quot;)
  print(f&quot;f2={f2}&quot;)
  </code></pre>
  <p>when I ran the script, I saw f1 and f2 are indeed two different functions by addresses. However, they have the same name &quot;f1&quot;. why?</p>
  <pre><code>&gt;test_copy_func2
  f1=&lt;function f1 at 0x00000208D43A1F28&gt;
  f2=&lt;function f1 at 0x00000208D43AEF28&gt;
  </code></pre>
  <p>(This was done on Windows 10 with Python 3.7.3)</p>
  <p>This caused a problem when I tried to pickle f2. My original program was to copy a local function f1 to a global function f2; and then pickle f2, because pickle works only on global functions, not on local functions. But the above issue makes pickle fail to work on f2, because f2 points to f1.</p>
  <p>How can I wipe out the trace of f1 from f2?</p>