Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'args': 0.05; 'args,': 0.09; 'func': 0.09; 'garbage': 0.09; 'referencing': 0.09; 'subject:Function': 0.09; 'def': 0.13; 'stored': 0.13; '*args,': 0.16; '\xa0def': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'jan': 0.19; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'holds': 0.21; 'subject:Question': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.24; 'function': 0.27; 'message-id:@mail.gmail.com': 0.28; 'partial': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'lines': 0.30; 'hi,': 0.32; 'skip:\xa0 30': 0.32; 'object': 0.33; 'fri,': 0.34; 'received:209.85.212': 0.34; '8bit%:3': 0.34; 'something': 0.35; 'later,': 0.37; 'but': 0.37; 'passed': 0.37; 'reference': 0.37; 'received:google.com': 0.37; 'references': 0.38; 'received:209.85': 0.38; 'skip:\xa0 10': 0.39; 'should': 0.39; 'being': 0.39; 'why': 0.39; 'received:209': 0.40; '8bit%:8': 0.40; 'below': 0.63; '2012': 0.67; 'collection,': 0.84; 'needed:': 0.84; 'skip:d 50': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=zWyQ1PMZaR4wpSZryteL/GFDABK7vrhH17IY6WnJj48=; b=G+e5K27PI3S1+vr8MCGvRbYmkNhhCj953FY8scgb74Z8GO3Vl1yhz3rUZJW2iEofQ2 rG9L7u5N4EULUOEZN8aCnQbeN+O8rmpbou6V2iEnYYgCGw+4DgeWqdoCVowrpMTQH9iU tjZt7tuclnxDhZpntGTTwvG5QXDO39mFX1mD4= MIME-Version: 1.0 In-Reply-To: <14889cd9-f02b-4a81-9ccb-8fb8492e1091@n39g2000yqh.googlegroups.com> References: <14889cd9-f02b-4a81-9ccb-8fb8492e1091@n39g2000yqh.googlegroups.com> From: Ian Kelly Date: Fri, 6 Jan 2012 15:04:20 -0700 Subject: Re: Nested Function Question To: GZ Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325887492 news.xs4all.nl 6851 [2001:888:2000:d::a6]:37387 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18623 On Fri, Jan 6, 2012 at 2:46 PM, GZ wrote: > Hi, > > I am reading the documentation of functools.partial (http:// > docs.python.org/library/functools.html#functools.partial) and found > the following 'reference implementation' of functools.partial. > > def partial(func, *args, **keywords): > =A0 =A0def newfunc(*fargs, **fkeywords): > =A0 =A0 =A0 =A0newkeywords =3D keywords.copy() > =A0 =A0 =A0 =A0newkeywords.update(fkeywords) > =A0 =A0 =A0 =A0return func(*(args + fargs), **newkeywords) > =A0 =A0newfunc.func =3D func > =A0 =A0newfunc.args =3D args > =A0 =A0newfunc.keywords =3D keywords > =A0 =A0return newfunc > > I don't understand why the below 3 lines are needed: > > =A0 =A0newfunc.func =3D func > =A0 =A0newfunc.args =3D args > =A0 =A0newfunc.keywords =3D keywords > > > It is as if they are trying to prevent garbage collection, but I don't > get why it is needed. As long as something holds reference to newfunc, > because it in turn references keywords and args, nothing will be > freed. If nothing is referencing newfunc, then everything should be > freed. They exist for introspection. The partial object has to store the function and arguments it was passed so that it can call it later, so as long as they're being stored anyway, why not make them visible?