Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.3 — *args, **kwargs and Unpacking in Calls
Python · Example 2 of 2

Forwarding and the shape of an SDK call

log_call works for any function. create imitates how an AI SDK takes a dict of options with **.

*args lets a function accept any number of positional arguments (collected into a tuple), and **kwargs any number of keyword arguments (collected into a dict). The same stars in a call do the opposite: they spread a list or dict into separate arguments, which is how wrappers forward arguments unchanged.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write average(*numbers) that returns the mean of any number of values, or 0 when none are given. Print average(2, 4, 9) and average().

02

Write make_url(host, **query) that returns https://host?key=value&key=value using all keyword arguments in order. Call make_url("example.com", page=2, sort="new").

03

Write call_twice(func, *args, **kwargs) that calls func two times with the same arguments and returns a list of both results. Test it with pow or max.

Explain it without notes

01

What do *args and **kwargs collect, and what types are they inside the function?

02

Explain what f(*items, **options) does in a call.

03

Why do wrapper functions take *args, **kwargs?

04

What is the order of the parameter kinds in a def line?

Forwarding and the shape of an SDK call
Sign in to run this example in your browser.

Expected output

calling power with (3,) {}
9
calling power with (2,) {'exponent': 10}
1024
125
{'model': 'small-model', 'messages': 1, 'options': {'temperature': 0.2, 'max_tokens': 100}}