Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18623

Re: Nested Function Question

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 <ian.g.kelly@gmail.com>
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 <ian.g.kelly@gmail.com>
Date Fri, 6 Jan 2012 15:04:20 -0700
Subject Re: Nested Function Question
To GZ <zyzhu2000@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4497.1325887492.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Fri, Jan 6, 2012 at 2:46 PM, GZ <zyzhu2000@gmail.com> 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):
>    def newfunc(*fargs, **fkeywords):
>        newkeywords = keywords.copy()
>        newkeywords.update(fkeywords)
>        return func(*(args + fargs), **newkeywords)
>    newfunc.func = func
>    newfunc.args = args
>    newfunc.keywords = keywords
>    return newfunc
>
> I don't understand why the below 3 lines are needed:
>
>    newfunc.func = func
>    newfunc.args = args
>    newfunc.keywords = 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?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Nested Function Question GZ <zyzhu2000@gmail.com> - 2012-01-06 13:46 -0800
  Re: Nested Function Question Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 15:04 -0700
  Re: Nested Function Question David Robinow <drobinow@gmail.com> - 2012-01-07 10:18 -0500
  Re: Nested Function Question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-07 20:43 -0800

csiph-web