Python Glossary Русский

all Python 3astterm

ast.SetComp

expressions

Node for a set comprehension {elt for … in … if …}: elt is the element expression, generators is a list of comprehension nodes.

Syntax

ast.SetComp(elt, generators)

Each for clause yields its own comprehension in generators, and if conditions live in its ifs field rather than as separate nodes. The loop variable inside is a Name with Store in its own scope, so it does not exist outside afterwards. It differs from Set only by having generators, and from DictComp by a single elt instead of a key and value pair.

Examples

import ast
n = ast.parse('{x * 2 for x in a for y in b if x}', mode='eval').body
print(type(n).__name__, len(n.generators), len(n.generators[1].ifs))  # → SetComp 2 1
print(ast.unparse(n.elt))  # → x * 2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy