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


Groups > comp.lang.python > #18622 > unrolled thread

Nested Function Question

Started byGZ <zyzhu2000@gmail.com>
First post2012-01-06 13:46 -0800
Last post2012-01-07 20:43 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#18622 — Nested Function Question

FromGZ <zyzhu2000@gmail.com>
Date2012-01-06 13:46 -0800
SubjectNested Function Question
Message-ID<14889cd9-f02b-4a81-9ccb-8fb8492e1091@n39g2000yqh.googlegroups.com>
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.

Thanks,
GZ

[toc] | [next] | [standalone]


#18623

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-01-06 15:04 -0700
Message-ID<mailman.4497.1325887492.27778.python-list@python.org>
In reply to#18622
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?

[toc] | [prev] | [next] | [standalone]


#18647

FromDavid Robinow <drobinow@gmail.com>
Date2012-01-07 10:18 -0500
Message-ID<mailman.4513.1325949503.27778.python-list@python.org>
In reply to#18622
On Fri, Jan 6, 2012 at 5:04 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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?
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.

[toc] | [prev] | [next] | [standalone]


#18660

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-01-07 20:43 -0800
Message-ID<33260053.1203.1325997828352.JavaMail.geo-discussion-forums@prak33>
In reply to#18622
GZ於 2012年1月7日星期六UTC+8上午5時46分16秒寫道:
> 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.
> 
> Thanks,
> GZ

This is used to produce a new function with some default parameters fixed
of  an old function that requires many parameters in the caller part. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web