7

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
halfer
  • 19,824
  • 17
  • 99
  • 186
capybaralet
  • 1,757
  • 3
  • 21
  • 31
  • 2
    Could you post the code that led to this error? – aensm Nov 21 '14 at 18:44
  • I suspect you forgot your function call parentheses somewhere, so your code is trying to pickle a method instead of its return value. – user2357112 Nov 21 '14 at 18:53
  • possible duplicate of [Understanding \_\_get\_\_ and \_\_set\_\_ and Python descriptors](http://stackoverflow.com/questions/3798835/understanding-get-and-set-and-python-descriptors) – GingerPlusPlus Nov 22 '14 at 11:45
  • 2
    I am not interested in debugging this error, and I found all the answers to these other questions unclear. I just want to know what a method_descriptor is, in plain English. – capybaralet Nov 27 '14 at 22:23

2 Answers2

5

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Cecil Curry
  • 9,789
  • 5
  • 38
  • 52
  • 2
    Oh, please. Stop calling descriptors voodoo. At least for a basic understanding it is enough to say that they override the member access operator (the dot, as in `obj.member`). And you can read a member, write to it, or delete it - hence the `__get__`, `__set__` and `__del__`. It is pretty much plain English – Alex Jan 21 '21 at 22:23
0

method_descriptor is a normal class with __get__, __set__ and __del__ methods. You can check the link for more info at Static vs instance methods of str in Python

Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40
JL-HaiNan
  • 1,004
  • 9
  • 20
  • I think the last code snippet in the link explains it (but I'm just pattern matching). The naming seems pretty bad, but I guess method_descriptor is just the type of a method that is attached to a class (only (as opposed to an instance of the class)). – capybaralet Apr 21 '18 at 23:10