Python Glossary Русский

all Python 3astterm

ast.LShift

operators

The marker for the binary operator << in an ast tree: it sits in BinOp.op and AugAssign.op and has no fields.

Syntax

ast.LShift()

'<<=' is not a node of its own: it is an AugAssign with the same LShift in op, so a search for shifts must look into both BinOp and AugAssign. Shift binds looser than addition: 'a << b + 1' yields BinOp(LShift) with BinOp(Add) on the right — ast.unparse shows it without extra parentheses.

Examples

import ast
b = ast.parse('a << b + 1', mode='eval').body
print(type(b.op).__name__, type(b.right.op).__name__)  # → LShift Add
aug = ast.parse('x <<= 2').body[0]
print(type(aug).__name__, type(aug.op).__name__)  # → AugAssign LShift

See also

Open in the full glossarydocs.python.orgReport an inaccuracy