Python Glossary Русский

all Python 3astterm

ast.Dict

literals

The node of a dict display {k: v}: two parallel lists, keys and values. An unpacking **d goes there too — with a None key.

Syntax

ast.Dict(keys, values)

Keys and values are stored separately, and pairs are rebuilt with zip(node.keys, node.values). An item {**other} yields a None key and the expression other as its value, so code that expects only nodes in keys breaks on None with AttributeError. A call dict(a=1) is not a Dict but a Call, and a comprehension {k: v for …} is a separate DictComp node.

Examples

import ast
node = ast.parse("{'a': 1, **extra}", mode='eval').body
print(type(node).__name__, len(node.keys))  # → Dict 2
print(node.keys[0].value, node.keys[1], node.values[1].id)  # → a None extra

See also

Open in the full glossarydocs.python.orgReport an inaccuracy