Python for AIPython 3.10+ · Pydantic 2.12
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
33 small wins to finish your pathNext lesson

Function arguments: defaults and keywords

A cost estimate needs the token count every time, but the price only when it differs from the usual one. Default values make an argument optional.

Example
def cost(tokens, price_per_1000=0.15):
    return round(tokens / 1000 * price_per_1000, 4)

print(cost(1200))
print(cost(1200, 2.5))
print(cost(1200, price_per_1000=2.5))

The value passed in a call is an argument. price_per_1000=0.15 in the definition means: use 0.15 when the caller does not pass one.

The last call names the argument. A keyword argument says what the value is for, which reads better than a bare 2.5 and lets you pass arguments in any order.

A required argument left out

Example
print(cost())

tokens has no default, so a call must give it. The message names the function and the argument that is missing.

Settings you pass along

Model calls take many optional settings, and a function that wraps one often passes them on without knowing their names. Two stars in front of a parameter collect every extra keyword argument into a dictionary.

Example
def call_model(prompt, **settings):
    print("prompt:", prompt)
    print("settings:", settings)

call_model("Sort this ticket")
call_model("Sort this ticket", temperature=0.2, max_tokens=50)

temperature and max_tokens are two settings most model APIs accept: how varied the answer is, and how long it may be. Here they only land in settings, but you will see **kwargs, the conventional name, in nearly every framework's source.

Try it yourself
  • Give cost a third parameter currency="USD" and return an f-string with it.
  • Call cost(price_per_1000=2.5, tokens=1200).
  • Print settings.get("temperature", 1.0) inside call_model.

You understood something today that you didn't yesterday.