easy +8 pts

Once Decorator: Run a Function Only Once

Build a decorator that ensures a function executes only once and returns the cached result.

Create a decorator `once` that wraps a function so that it runs only on the first call. On the first call, it executes the original function and stores the return value. On every subsequent call, it does NOT re-execute the function but returns the previously stored result. The wrapper must accept any number of positional and keyword arguments. The original function should be accessed via `__wrapped__` (set by `functools.wraps`). Implement the decorator `once` as a function that takes a function and returns a wrapper. The test suite will call the decorated functions directly (e.g., `make_greet("Alice")`). Your implementation of `once` must correctly handle these decorated functions.

Constraints

The decorated function may accept any number of positional and keyword arguments. All arguments are ignored after the first call. Assume the original function has no side effects that must repeat. Do not worry about concurrency.

Example

```python
@once
def make_greet(name):
    print("Computing...")
    return f"Hello, {name}!"

make_greet("Alice")  # prints Computing... and returns "Hello, Alice!"
make_greet("Bob")    # no print, returns "Hello, Alice!" again

@once
def make_counter():
    return 1

make_counter()  # returns 1
make_counter()  # returns 1 still

@once
def make_add(a, b):
    return a + b

make_add(2, 3)  # returns 5
make_add(4, 5)  # returns 5 (first result)
```
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a nonlocal variable to store the result and a flag to track if already called.
Use functools.wraps(func) to decorate the wrapper.
If already called, return the stored result without calling func again.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.