Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Created November 24, 2011 18:47
Show Gist options
  • Save lvidarte/1391997 to your computer and use it in GitHub Desktop.
Save lvidarte/1391997 to your computer and use it in GitHub Desktop.
Using update_wrapper() copies or adds attributes from the original function to the partial object
import functools
def foo(a, b=2):
"""Docstring for foo()."""
print "called foo with:", (a, b)
def show_details(name, f):
print "%s:" % name
print "object:", f
print "__name__:",
try:
print f.__name__
except AttributeError:
print "(no __name__)"
print "__doc__:", repr(f.__doc__)
print
show_details('foo', foo)
p1 = functools.partial(foo, b=4)
show_details('raw wrapper', p1)
print "Updating wrapper:"
print "assign:", functools.WRAPPER_ASSIGNMENTS
print "update:", functools.WRAPPER_UPDATES
print
functools.update_wrapper(p1, foo)
show_details('updated wrapper', p1)
foo:
object: <function foo at 0x7f985e88f7d0>
__name__: foo
__doc__: 'Docstring for foo().'
raw wrapper:
object: <functools.partial object at 0x7f985e8e1fc8>
__name__: (no __name__)
__doc__: 'partial(func, *args, **keywords) - new function with partial application\n of the given arguments and keywords.\n'
Updating wrapper:
assign: ('__module__', '__name__', '__doc__')
update: ('__dict__',)
updated wrapper:
object: <functools.partial object at 0x7f985e8e1fc8>
__name__: foo
__doc__: 'Docstring for foo().'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment