Nested Function Question

G

GZ

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
 
I

Ian Kelly

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?
 
D

David Robinow

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.
 
8

88888 Dihedral

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top