Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, what is a method_descriptor?

Tags:

python

methods

In Python, what is a method_descriptor (in plain English)?

I had this error, and I can't really find any information on it:

*** TypeError: can't pickle method_descriptor objects
like image 943
capybaralet Avatar asked Nov 21 '14 18:11

capybaralet


1 Answers

Switch to dill.

I am not interested in debugging this error...

You should be. If you're uninterested in debugging errors, you're in the wrong field. For the sake of polite argumentation, let's charitably assume you authored that comment under the duress of an unreasonable deadline. (It happens.)

The standard pickle module is incapable of serializing so-called "exotic types," including but presumably not limited to: functions with yields, nested functions, lambdas, cells, methods, unbound methods, modules, ranges, slices, code objects, methodwrapper objects, dictproxy objects, getsetdescriptor objects, memberdescriptor objects, wrapperdescriptor objects, notimplemented objects, ellipsis objects, quit objects, and (...wait for it!) method_descriptor objects.

All is not lost, however. The third-party dill package is capable of serializing all of these types and substantially more. Since dill is a drop-in replacement for pickle, globally replacing all calls across your codebase to the pickle.dump() function with the equivalent dill.dump() function should suffice to pickle the problematic method descriptors in question.

I just want to know what a method_descriptor is, in plain English.

No, you don't. There is no plain-English explanation of method descriptors, because the descriptor protocol underlying method descriptors is deliciously dark voodoo.

It's voodoo, because it has to be; it's the fundamental basis for Python's core implementation of functions, properties, static methods, and class methods. It's dark, because only a dwindling cabal of secretive Pythonistas are actually capable of correctly implementing a descriptor in the wild. It's delicious, because the power that data descriptors in particular provide is nonpareil in the Python ecosystem.

Fortunately, you don't need to know what method descriptors are to pickle them. You only need to switch to dill.

like image 200
Cecil Curry Avatar answered Sep 26 '22 03:09

Cecil Curry