title: What is the proper way to make an object with unpickable fields pickable?
body: <p>For me what I do is detect what is unpickable and make it into a string (I guess I could have deleted it too but then it will falsely tell me that field didn't exist but I'd rather have it exist but be a string). But I wanted to know if there was a less hacky more official way to do this.</p>
  <p>Current code I use:</p>
  <pre><code>def make_args_pickable(args: Namespace) -&gt; Namespace:
      &quot;&quot;&quot;
      Returns a copy of the args namespace but with unpickable objects as strings.
  
      note: implementation not tested against deep copying.
      ref:
          - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
      &quot;&quot;&quot;
      pickable_args = argparse.Namespace()
      # - go through fields in args, if they are not pickable make it a string else leave as it
      # The vars() function returns the __dict__ attribute of the given object.
      for field in vars(args):
          field_val: Any = getattr(args, field)
          if not dill.pickles(field_val):
              field_val: str = str(field_val)
          setattr(pickable_args, field, field_val)
      return pickable_args
  </code></pre>
  <p>Context: I think I do it mostly to remove the annoying tensorboard object I carry around (but I don't think I will need the <code>.tb</code> field anymore thanks to <code>wandb</code>/<a href="https://wandb.ai/site" rel="nofollow noreferrer">weights and biases</a>). Not that this matters a lot but context is always nice.</p>
  <p>Related:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/3603581/what-does-it-mean-for-an-object-to-be-picklable-or-pickle-able">What does it mean for an object to be picklable (or pickle-able)?</a></li>
  <li><a href="https://stackoverflow.com/questions/30699391/python-how-can-i-make-this-un-pickleable-object-pickleable">Python - How can I make this un-pickleable object pickleable?</a></li>
  </ul>
  <hr />
  <p>Edit:</p>
  <p>Since I decided to move away from dill - since sometimes it cannot recover classes/objects (probably because it cannot save their code or something) - I decided to only use <code>pickle</code> (which seems to be the recommended way to be done in PyTorch).</p>
  <p>So what is the official (perhaps optimized) way to check for pickables without dill or with the official pickle?</p>
  <p>Is this the best:</p>
  <pre><code>def is_picklable(obj):
    try:
      pickle.dumps(obj)
  
    except pickle.PicklingError:
      return False
    return True
  </code></pre>
  <p>thus current soln:</p>
  <pre><code>def make_args_pickable(args: Namespace) -&gt; Namespace:
      &quot;&quot;&quot;
      Returns a copy of the args namespace but with unpickable objects as strings.
  
      note: implementation not tested against deep copying.
      ref:
          - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
      &quot;&quot;&quot;
      pickable_args = argparse.Namespace()
      # - go through fields in args, if they are not pickable make it a string else leave as it
      # The vars() function returns the __dict__ attribute of the given object.
      for field in vars(args):
          field_val: Any = getattr(args, field)
          # - if current field value is not pickable, make it pickable by casting to string
          if not dill.pickles(field_val):
              field_val: str = str(field_val)
          elif not is_picklable(field_val):
              field_val: str = str(field_val)
          # - after this line the invariant is that it should be pickable, so set it in the new args obj
          setattr(pickable_args, field, field_val)
      return pickable_args
  
  
  def make_opts_pickable(opts):
      &quot;&quot;&quot; Makes a namespace pickable &quot;&quot;&quot;
      return make_args_pickable(opts)
  
  
  def is_picklable(obj: Any) -&gt; bool:
      &quot;&quot;&quot;
      Checks if somehting is pickable.
  
      Ref:
          - https://stackoverflow.com/questions/70128335/what-is-the-proper-way-to-make-an-object-with-unpickable-fields-pickable
      &quot;&quot;&quot;
      import pickle
      try:
          pickle.dumps(obj)
      except pickle.PicklingError:
          return False
      return True
  </code></pre>
  <p>Note: one of the reasons I want something &quot;offical&quot;/tested is because I am getting pycharm halt on the try catch: <a href="https://stackoverflow.com/questions/70761481/how-to-stop-pycharms-break-stop-halt-feature-on-handled-exceptions-i-e-only-b?noredirect=1&amp;lq=1">How to stop PyCharm&#39;s break/stop/halt feature on handled exceptions (i.e. only break on python unhandled exceptions)?</a> which is not what I want...I want it to only halt on unhandled exceptions.</p>