Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101386

What use is '__reduce__'?

Newsgroups comp.lang.python
Date 2016-01-08 08:42 -0800
Message-ID <399e214a-552b-4d4a-a39f-8e917dadd6c6@googlegroups.com> (permalink)
Subject What use is '__reduce__'?
From Robert <rxjwg98@gmail.com>

Show all headers | View raw


Hi,

When I try to run the following code:



/////
from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first seen'
     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__,
                            OrderedDict(self))
     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

oc = OrderedCounter('abracadabra') 
-----

I don't know the use of '__reduce__', even I look it up on Python website.
On that website, it explains 'pickle' module:
https://docs.python.org/2/library/pickle.html

But the above example without import that module. Is it from built-in?
Anyhow, I don't find a built-in explanation about '__reduce__'.

What use of the above two new self methods are in class OrderedCounter?

Thanks,

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

What use is '__reduce__'? Robert <rxjwg98@gmail.com> - 2016-01-08 08:42 -0800
  Re: What use is '__reduce__'? Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-08 10:03 -0700
    Re: What use is '__reduce__'? Robert <rxjwg98@gmail.com> - 2016-01-08 09:45 -0800
  Re: What use is '__reduce__'? Peter Otten <__peter__@web.de> - 2016-01-08 21:12 +0100

csiph-web