How to check if an object is pickleable

后端 未结 3 1881
忘掉有多难
忘掉有多难 2020-12-18 19:47

I have a list of objects of various types that I want to pickle. I would like to pickle only those which are pickleable. Is there a standard way to check if an object is of

3条回答
  •  执念已碎
    2020-12-18 20:16

    dill allows for pickling more things that the builtin pickle.

    This should do what you what, I think:

    def is_picklable(obj):
      try:
        pickle.dumps(obj)
    
      except pickle.PicklingError:
        return False
      return True
    

提交回复
热议问题