Python Glossary Русский

all Python 3astterm

ast.Set

literals

Node for a set display {a, b}: the elts field is the list of element expressions.

Syntax

ast.Set(elts)

Empty braces {} are a Dict, and an empty set is written set(), which parses as a Call, so a Set node always has at least one element. {x for x in a} is a SetComp instead. Unlike List and Tuple, Set has no ctx field because a set cannot be an assignment target.

Examples

import ast
s = ast.parse('{1, x}', mode='eval').body
print(type(s).__name__, len(s.elts))  # → Set 2
for src in ('{}', 'set()'):
    print(type(ast.parse(src, mode='eval').body).__name__)  # → Dict / Call

See also

Open in the full glossarydocs.python.orgReport an inaccuracy