Skip to content
Search

Python:functools

lastmod 2024-01-06created 2021-04-136 edits

functools.wraps

Makes a decorator's wrapper look like the original function, making debugging and documentation convenient.

functools.partial

It is convenient to think of it as the same feature as Boost:Bind.

def power(base, exponent):
    return base ** exponent

def square(base):
    return power(base, 2)

def cube(base):
    return power(base, 3)

assert square(2) == 4
assert cube(2) == 8

The code above can be converted as follows.

from functools import partial

def power(base, exponent):
    return base ** exponent

square = partial(power, exponent=2)
cube = partial(power, exponent=3)

assert square(2) == 4
assert cube(2) == 8

See also

References


  1. Velog.io_-doondoony-_Python_functools.wraps.pdf ↩

author Zer0 sitemap