Basic usage
Input
f = limit_calls(2)(lambda x: x + 1); f(3)
Output
4
f is allowed 2 calls. The first call returns 3 + 1 = 4.
Full lesson preview
Implement a decorator factory that limits how many times a function can be called and returns a default value thereafter.
Problem statement
Task
Examples
Input
f = limit_calls(2)(lambda x: x + 1); f(3)
Output
4
f is allowed 2 calls. The first call returns 3 + 1 = 4.
Input
(g := limit_calls(1, default='X')(lambda: 'ok'), g(), g())[2]
Output
X
g is allowed 1 successful call (returns 'ok'); the second call returns the default 'X'.
Input format
Output format
Constraints
Samples
Input
f = limit_calls(2)(lambda x: x*2); f(5)
Output
10
Two calls allowed; first call returns 5*2 = 10.