Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: What use is '__reduce__'? Date: Fri, 8 Jan 2016 10:03:21 -0700 Lines: 41 Message-ID: References: <399e214a-552b-4d4a-a39f-8e917dadd6c6@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de fXy8MwhS4ZMiwO5p7x92EwyTiV/38AupmOVOGAYUA61g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'collections': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'missed': 0.15; "'%s(%r)'": 0.16; '2016': 0.16; 'anyhow,': 0.16; 'copied.': 0.16; 'module:': 0.16; 'ordereddict': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'remembers': 0.16; 'wrote:': 0.16; 'example.': 0.18; 'am,': 0.23; 'elements': 0.23; 'thanks,': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'module.': 0.27; "skip:' 10": 0.28; 'skip:( 20': 0.28; '-----': 0.29; 'code:': 0.29; 'url:mailman': 0.30; 'code': 0.30; 'probably': 0.31; 'included': 0.32; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'url:python': 0.33; 'instances': 0.33; 'url:listinfo': 0.34; 'received:google.com': 0.35; 'robert': 0.35; 'subject:use': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'received:209.85.213': 0.37; "won't": 0.38; 'received:209': 0.38; 'self': 0.38; 'skip:o 20': 0.38; 'hi,': 0.38; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'determine': 0.61; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=yphN81GdTyDX1eLT03wYoQGHopWTeysCueoVA5IYwJ8=; b=ifrAmL6zHiepB60zo8lktslNFLvSQvABbL288UtRhaI39FQ3P9YX1Ty5cNBEwYhfFd JeC9aQ08xVJBuAERfGDSFbMEg6zxuI1dO24/2dPD/sEDdALjYt1jc6iEAvGu1FigF9Mh wBg1TJuugm6SG/MnNR+GnhviXqx641BBhYlg37RNPRwZWqEmInVJgtyF/o+FvJCgJraT TeUBUkXq5aguxD9IiZYG2DHjTtX5u8T1ULFWCbjO7NyfMZbODcASNlKUTy2OhWxejcJy NUlbpHlZSjCRddlWzqJ1t7UXEGWn8N0ZUiRmb7jdbDrdPxTSvZ6qnyLfgz750CE/bqI3 waIw== X-Received: by 10.50.61.177 with SMTP id q17mr21989545igr.68.1452272640317; Fri, 08 Jan 2016 09:04:00 -0800 (PST) In-Reply-To: <399e214a-552b-4d4a-a39f-8e917dadd6c6@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101388 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 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