The constructor for functools.partial()
detects nesting and automatically flattens itself
to a more efficient form. For example:
from functools import partial
def f(*, a: int, b: int, c: int) -> None:
print(f"Args are {a}-{b}-{c}")
g = partial(partial(partial(f, a=1), b=2), c=3)
# Three function calls are flattened into one; free efficiency.
print(g)
# Bare function can be called as 3 arguments were bound previously.
g()
This returns:
functools.partial(<function f at 0x7f4fd16c11f0>, a=1, b=2, c=3)
Args are 1-2-3
~~~
Recent posts
- Preventing accidental struct copies in Go
- Go 1.24's "tool" directive
- Capturing console output in Go tests
- Deferred teardown closure in Go testing
- Three flavors of sorting Go slices
- Nil comparisons and Go interface
- Stacked middleware vs embedded delegation in Go
- Why does Go's io.Reader have such a weird signature?
- Go slice gotchas
- The domain knowledge dilemma