Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Functions and parameters

For an ordinary undecorated function, def binds its name to the function and a call executes its body. A generator function call creates a generator, and a decorator can bind the name to another object.

Version: Python 3.14; repository image, production runtime not attested

Syntax

def total(values, /, *, start=0):
    return sum(values, start)

For an ordinary undecorated function, def binds its name to the function and a call executes its body. A generator function call creates a generator, and a decorator can bind the name to another object.

Parameters before / are positional-only and those after a bare * are keyword-only. Defaults are evaluated at definition time, so a default list retains mutations across calls. Falling off the end returns None.

total returns 5 and 7. Passing start positionally raises TypeError.

def history(n, seen=[]):
    seen.append(n)
    return tuple(seen)
assert history(1)==(1,) and history(2)==(1,2)
def fresh(n,seen=None):
    if seen is None: seen=[]
    seen.append(n)
    return seen
assert fresh(1)==[1] and fresh(2)==[2]
def no_result(): pass
assert no_result() is None
effects=[]
def gen():
    effects.append('body')
    yield 4
it=gen()
assert effects==[] and next(it)==4 and effects==['body']
def replacement(f): return 'decorated'
@replacement
def f(): return 1
assert f=='decorated'

Examples

def total(values, /, *, start=0):
    return sum(values, start)

print(total([2, 3]), total([], start=7))
try:
    total([2], 7)
except TypeError as error:
    print(type(error).__name__)

Result

5 7
TypeError

total returns 5 and 7. Passing start positionally raises TypeError.

Sources