Created
          November 24, 2011 18:47 
        
      - 
      
 - 
        
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
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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__) | |
| 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 | |
| functools.update_wrapper(p1, foo) | |
| show_details('updated wrapper', p1) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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