The documentation for Pickle specifically says:
Instances of a new-style class C are created using:
obj = C.__new__(C, *args)
Attempting to take advantage of this, I created a singleton with no instance attributes or methods:
class ZeroResultSentinel(object):instance = Nonedef __new__(cls, *args):if not cls.instance:cls.instance = super(ZeroResultSentinel, cls).__new__(cls, *args)return cls.instance
(This class is used in a caching layer to differentiate a no-result result from nothing in the cache.)
The singleton works great (every call to ZeroResultSentinel()
results in the same instance in memory, and ZeroResultSentinel() == ZeroResultSentinel()
evaluates to True
). And I can pickle and unpickle the instance without errors. However, when I unpickle it, I get a different instance. So I placed a breakpoint within __new__
. I hit the breakpoint every time I call ZeroResultSentinel()
, but I do not hit a breakpoint when I unpickle a pickled ZeroResultSentinel
. This is in direct contradiction to the documentation. So am I doing something wrong, or is the documentation incorrect?