Can’t pickle defaultdict

In addition to Martijn’s explanation: A module-level function is a function which is defined at module level, that means it is not an instance method of a class, it’s not nested within another function, and it is a “real” function with a name, not a lambda function. So, to pickle your defaultdict, create it with … Read more

Hitting Maximum Recursion Depth Using Pickle / cPickle

From the docs: Trying to pickle a highly recursive data structure may exceed the maximum recursion depth, a RuntimeError will be raised in this case. You can carefully raise this limit with sys.setrecursionlimit(). Although your trie implementation may be simple, it uses recursion and can lead to issues when converting to a persistent data structure. … Read more

Pickle or json?

I prefer JSON over pickle for my serialization. Unpickling can run arbitrary code, and using pickle to transfer data between programs or store data between sessions is a security hole. JSON does not introduce a security hole and is standardized, so the data can be accessed by programs in different languages if you ever need … Read more

Python serialization – Why pickle?

Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script. As for where the pickled information is stored, usually one would do: with open(‘filename’, ‘wb’) as f: var = … Read more