Python Glossary Русский

all Python 3astterm

ast.Tuple

literals

Node for a tuple: elts holds the items, ctx is Load or Store. A comma creates it, not parentheses: both 1, 2 and the target a, b = … are Tuple.

Syntax

ast.Tuple(elts, ctx=ast.Load())

(1) parses as Constant while 1, parses as Tuple: the comma makes the tuple. Tuple also appears where no parentheses are visible: d[1, 2] gives a Subscript whose slice is a tuple, return a, b gives Return with a Tuple, and a, b = b, a has a Tuple on both sides (Store and Load). An empty () is a Tuple with empty elts.

Examples

import ast
for src in ('(1)', '1,', '()'):
    print(type(ast.parse(src, mode='eval').body).__name__)  # → Constant / Tuple / Tuple
import ast
n = ast.parse('a, b = b, a').body[0]
print(type(n.targets[0].ctx).__name__, type(n.value.ctx).__name__)  # → Store Load
print(type(ast.parse('d[1, 2]', mode='eval').body.slice).__name__)  # → Tuple

See also

Open in the full glossarydocs.python.orgReport an inaccuracy