Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101386 > unrolled thread
| Started by | Robert <rxjwg98@gmail.com> |
|---|---|
| First post | 2016-01-08 08:42 -0800 |
| Last post | 2016-01-08 21:12 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-08 08:42 -0800 |
| Subject | What use is '__reduce__'? |
| Message-ID | <399e214a-552b-4d4a-a39f-8e917dadd6c6@googlegroups.com> |
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,
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-01-08 10:03 -0700 |
| Message-ID | <mailman.76.1452272642.2305.python-list@python.org> |
| In reply to | #101386 |
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 <rxjwg98@gmail.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
[toc] | [prev] | [next] | [standalone]
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-08 09:45 -0800 |
| Message-ID | <a0f52166-711f-40c4-8631-7b934e443a43@googlegroups.com> |
| In reply to | #101388 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-08 21:12 +0100 |
| Message-ID | <mailman.79.1452283950.2305.python-list@python.org> |
| In reply to | #101386 |
Ian Kelly 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.
__reduce__() also makes copying work as expected:
>>> 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')
>>> import copy
>>> copy.copy(oc)
OrderedCounter(OrderedDict([('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d',
1)]))
Now take away the __reduce__ method:
>>> del OrderedCounter.__reduce__
>>> copy.copy(oc)
OrderedCounter(OrderedDict([('b', 2), ('a', 5), ('c', 1), ('r', 2), ('d',
1)]))
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web