Python Glossary Русский

all Python 3Tuples (tuple)term

Returning a tuple from a function

usage

A function can return several values — Python packs them into a tuple.

Syntax

return a, b, c  # ≡ return (a, b, c)

Nothing genuinely multi-valued happens here: one tuple goes back, and the unpacking at the call site takes it apart. That is where the classic break comes from — add a fourth value to the return and every existing a, b, c = f() raises ValueError. Past three values, or when the order is easy to mix up, a NamedTuple or dataclass is safer, because callers read fields by name instead of by position.

Examples

def minmax(lst):
    return min(lst), max(lst)
lo, hi = minmax([3, 1, 4, 1, 5, 9])
print(lo, hi)
# → 1 9
def divmod_ex(a, b):
    return a // b, a % b
q, r = divmod_ex(17, 5)
print(q, r)
# → 3 2
def parse_point(s):
    x, y = s.split(',')
    return int(x), int(y)
print(parse_point('3,4'))
# → (3, 4)
result = minmax([3, 1, 4])
print(type(result))
# → <class 'tuple'>
def triple(x):
    return x, x*2, x*3
a, b, c = triple(5)
print(a, b, c)
# → 5 10 15

See also

Open in the full glossarydocs.python.orgReport an inaccuracy