functools puzzle

George Trojan - NOAA Federal george.trojan at noaa.gov
Wed Apr 6 13:57:34 EDT 2016


Here is my test program:

''' generic test '''

import functools
import inspect

def f():
    '''I am f'''
    pass

g = functools.partial(f)
g.__doc__ = '''I am g'''
g.__name__ = 'g'

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

h = partial(f)
functools.update_wrapper(h, f)
h.__doc__ = '''I am h'''
h.__name__ = 'h'

print(type(g), g.__name__, inspect.getdoc(g))
print(type(h), h.__name__, inspect.getdoc(h))
help(g)
help(h)

The output is what I expect:

(devenv-3.5.1) dilbert at gtrojan> python x.py
<class 'functools.partial'> g I am g
<class 'function'> h I am h

However the help screens for h and g are different. help(h) is again what I
expect:

Help on function h in module __main__:

h()
    I am h

But help(g) is (almost) identical to help(functools.partial), the doc
string disappears:

Help on partial object:

g = class partial(builtins.object)
 |  partial(func, *args, **keywords) - new function with partial application
 |  of the given arguments and keywords.
...

The module functools has partial() defined as above, then overrides the
definition by importing partial from _functools. That would explain the
above behaviour. My question is why?

George



More information about the Python-list mailing list