Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.006 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; 'seems': 0.20; '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; 'code': 0.25; 'function': 0.27; 'cc:addr:gmail.com': 0.28; 'tend': 0.28; 'message-id:@mail.gmail.com': 0.28; 'partial': 0.29; 'toward': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'lines': 0.30; 'kelly': 0.30; 'hi,': 0.32; 'skip:\xa0 30': 0.32; 'object': 0.33; 'fri,': 0.34; '8bit%:3': 0.34; 'received:74.125.82': 0.35; 'something': 0.35; 'cc:2**1': 0.36; 'later,': 0.37; 'but': 0.37; 'passed': 0.37; 'reference': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; 'references': 0.38; 'skip:\xa0 10': 0.39; 'should': 0.39; 'being': 0.39; 'why': 0.39; '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:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=OfmoaMssRtWugwdqJ2b20ybFAMRjO1dziUBTehZqNMw=; b=vsjPY0TZ0jjKRdrDoUOfTHUv4SsZild/xeYeQvrr+Hxgl29G2/kUNbz6cefMXeM+Hm WYHv5okX7Cw8HKXh9Dj904MLE5r1fWPKjL1/7ATuCtq6EJDHxU+dTtxHzRWcuTT4AcO0 LSvl6nbQa+f4aeXhNYETNmca6zfbmIIBshKcc= MIME-Version: 1.0 In-Reply-To: References: <14889cd9-f02b-4a81-9ccb-8fb8492e1091@n39g2000yqh.googlegroups.com> Date: Sat, 7 Jan 2012 10:18:15 -0500 Subject: Re: Nested Function Question From: David Robinow To: Ian Kelly Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: GZ , 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325949503 news.xs4all.nl 6918 [2001:888:2000:d::a6]:40435 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18647 On Fri, Jan 6, 2012 at 5:04 PM, Ian Kelly wrote: > 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. =A0The 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? I tend toward the minimalist end of the spectrum when it comes to code commenting, but it seems to me this would be a good place for a few words of explanation.