Neil G
Neil G

Reputation: 33252

What is the default `__reduce__` in Python?

import pickle

class A:
    pass

pickle.dumps(B().__reduce__())

yields

(<function _reconstructor at 0x1010143b0>, (<class '__main__.B'>, <class 'object'>, None))

What is this function "_reconstructor". It's neither B, B.__init__, nor B.__new__ as I expected.

Upvotes: 0

Views: 3501

Answers (1)

Tim Peters
Tim Peters

Reputation: 70715

I have to make 2 changes to get that result:

  1. Change the name of your class from A to B.

  2. Remove the outer pickle.dumps() call.

In any case, pickle is free to do anything it likes to reconstruct the object ;-) In this case, you can find the _reconstructor() function in Lib/copyreg.py.

Upvotes: 1

Related Questions