functools.update_wrapper in Python

In Python, the update_wrapper is a function of the functools module that updates a wrapper function to look like a wrapped function. In other words, it will update the metadata like __module__, __name__, __doc__ of a wrapper function to look like the wrapped function.

This function can be used with a callable other than the functions. Moreover, any attributes named in assigned or updated that are missing from an object being wrapped will be ignored.

Syntax

The signature for the update_wrapper function is as shown below. Here, WRAPPER_ASSIGNMENTS can be __module__, __name__, __doc__ whereas WRAPPER_UPDATES can be __dict__.

functools.update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

Python update_wrapper Examples:

Example 1: In this case, we are updating the wrappers for documentation and name for the parent function multi.

from functools import partial,update_wrapper

#Multiplication function
def multi(x,y):
    '''Computing Multiplication of x and y'''
    return x*y
    
#partial functions
mul2 = partial(multi, y=2)
print("2*5= ",mul2(5))

mul2.__doc__='''***This function computes the multiplication of two numbers'''
mul2.__name__ = 'product'

print('Documentation of mul2 :', mul2.__doc__) 
print('Name of mul2 function is  :', mul2.__name__)
update_wrapper(mul2, multi)
print('Documentation of multi :', multi.__doc__) 
print('Name of multi is  :', multi.__name__)

Output

2*5=  10
Documentation of mul2 : ***This function computes the multiplication of two numbers
Name of mul2 function is  : product
Documentation of multi : Computing Multiplication of x and y
Name of multi is  : multi

Example 2: In this case, we are first printing the documentation written in code. Thereafter, it gets the documentation of math.pow built-in function.

from functools import partial,update_wrapper
import math
import functools
#partial functions
p2=functools.partial(math.pow,5)
print(p2(2))

p2.__doc__='''computing power of 2 ^ 5'''
p2.__name__ = 'power function'

print('Documentation of p2 :', p2.__doc__) 
print('Name of p2 function is  :', p2.__name__)
update_wrapper(p2, math.pow)
print('Documentation of math.pow :', math.pow.__doc__) 
print('Name of math.pow is  :', math.pow.__name__)

Output

25.0
Documentation of p2 : ***This function computes the multiplication of two numbers
Name of p2 function is  : product
Documentation of multi : Return x**y (x to the power of y).
Name of multi is  : pow

Example 3: In this case, we are updating the wrappers for documentation and name for the parent function simpleint.

from functools import partial,update_wrapper
import math
import functools
#partial functions
def simpleint(p,r,t):
    '''****Computing Simple Interest****'''
    return (p*r*t)/100.0
 
si = partial(simpleint,7,t=3)
print("Simple interest is ",si(10000))
si.__doc__='''Computing simple interest'''
si.__name__ = 'SI'

print('Documentation of si :', si.__doc__) 
print('Name of si function is  :', si.__name__)
update_wrapper(si, simpleint)
print('Documentation of simpleint :', simpleint.__doc__) 
print('Name of simpleint is  :', simpleint.__name__)

Output

Simple interest is  2100.0
Documentation of si : Computing simple interest
Name of si function is  : SI
Documentation of simpleint : ****Computing Simple Interest****
Name of simpleint is  : simpleint

Conclusion

Hence, We can use the update_wrapper function to update the wrapper function since Python version 3.2.

References

Happy Learning 🙂

Leave A Comment