title: Serialize a Python method with global variables by dill
body: <p>Here's an example code where <code>FLAG</code> is a global variable. The method <code>A.func</code> is a blackbox to me so that I don't know it calls <code>FLAG</code> before serialization.</p>
  
  <pre><code>import dill as pickle
  FLAG = 100
  
  class A:
    def func(self):
      print FLAG * 10
  
  a = A()
  dump = pickle.dumps(a.func)
  del FLAG
  foo = pickle.loads(dump) &lt;-- fail here "NameError: global name 'FLAG' is not defined"
  foo()
  </code></pre>
  
  <p>In related questions: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/26389981/serialize-a-python-function-with-dependencies">Serialize a python function with dependencies</a></li>
  <li><a href="https://stackoverflow.com/questions/10048061/how-to-pickle-a-python-function-with-its-dependencies">How to pickle a python function with its dependencies?</a></li>
  </ul>
  
  <p>The most practical solution is using <code>cloudpickle</code>.  But it seems that <code>dill</code> is more robust than <code>cloudpickle</code>. So I'd like to stick to <code>dill</code> or other mature picklers.</p>
  
  <p>I don't mind to modify some <code>dill</code> code by myself if necessary.</p>
  
  <p>Thanks for any help in advance :) </p>