Python Glossary Русский

all Python 3astterm

ast.Pow

operators

The exponentiation operator ** in the op field of BinOp and AugAssign nodes.

Syntax

ast.Pow()

** is the only right-associative binary operator: 2 ** 3 ** 2 parses as 2 ** (3 ** 2), so the nested BinOp is on the right, not the left. It binds tighter than a unary minus on its left: -2 ** 2 is a UnaryOp over a BinOp. The same Pow appears in x **= 2, but inside AugAssign; the builtin pow() is a plain Call.

Examples

import ast
n = ast.parse('2 ** 3 ** 2', mode='eval').body
print(type(n.op).__name__, type(n.left).__name__, type(n.right).__name__)  # → Pow Constant BinOp
print(2 ** 3 ** 2)  # → 512
import ast
s = ast.parse('x **= 2').body[0]
print(type(s).__name__, type(s.op).__name__)  # → AugAssign Pow

See also

Open in the full glossarydocs.python.orgReport an inaccuracy