Pylerno
English

Parameters and defaults

Design calls that are clear and avoid shared mutable defaults.

On this page

The concept

Arguments can be passed by position or by name. Keyword arguments document intent at the call site. Default values make selected arguments optional, but each default is evaluated when the function is defined, not on every call.

A list used as a default can therefore be shared between calls accidentally. Use None as a sentinel and create a new list inside the function. A bare * in the parameter list makes later parameters keyword-only, preventing an unexplained positional number from being mistaken for another setting.

Worked example

python
def greet(name, greeting="Hello"):
    return f"{greeting} {name}"


def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items


def final_price(price, *, discount=0):
    return price * (1 - discount)


print(greet("Dana"))
print(greet(greeting="Welcome", name="Sara"))
print(add_item("apple"))
print(add_item("banana"))
print(final_price(100, discount=0.15))

Trace the result

  1. greet uses a default greeting unless the caller names another one.
  2. add_item creates a new list when items is None, so the two demonstration calls remain independent.
  3. final_price requires discount to be named, making the call easier to read.
text
Hello Dana
Welcome Sara
['apple']
['banana']
85.0
Line-by-line walkthrough

Lines 1–2: def greet(name, greeting="Hello"):

Define the function greet with input names name, greeting. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 2: return f"{greeting} {name}"

Evaluate f'{greeting} {name}' and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 5–9: def add_item(item, items=None):

Define the function add_item with input names item, items. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Lines 6–7: if items is None:

Evaluate items is None. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 7: items = []

Evaluate [], then bind or update items with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 8: items.append(item)

Call items.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.

Line 9: return items

Evaluate items and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Lines 12–13: def final_price(price, *, discount=0):

Define the function final_price with input names price, discount. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.

Line 13: return price * (1 - discount)

Evaluate price * (1 - discount) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.

Line 16: print(greet("Dana"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 17: print(greet(greeting="Welcome", name="Sara"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 18: print(add_item("apple"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 19: print(add_item("banana"))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 20: print(final_price(100, discount=0.15))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Never use an empty list default merely to save a line. That list persists between calls and may retain earlier input.

Check your understanding

What does this program print?

python
def add(n, step=2):
    return n + step
print(add(5, 1))
Choose one answer

Search documentation

Share this page