What use is '__reduce__'?

Robert rxjwg98 at gmail.com
Fri Jan 8 12:45:58 EST 2016


On Friday, January 8, 2016 at 12:04:21 PM UTC-5, Ian wrote:
> As you found by searching, __reduce__ is used to determine how
> instances of the class are pickled. If the example you're using
> doesn't do any pickling, then it's not really relevant to the example.
> It's probably included so that it won't be missed when the code is
> copied.
> 
> On Fri, Jan 8, 2016 at 9:42 AM, Robert <ail.com> wrote:
> > 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,
> >
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list

Thanks for your reply.



More information about the Python-list mailing list