NamedExpr

class astroid.nodes.NamedExpr(lineno: int | None = None, col_offset: int | None = None, parent: NodeNG | None = None, *, end_lineno: int | None = None, end_col_offset: int | None = None)[source]

Bases: AssignTypeNode

Represents the assignment from the assignment expression

>>> import astroid
>>> module = astroid.parse('if a := 1: pass')
>>> module.body[0].test
<NamedExpr l.1 at 0x...>
assigned_stmts(**kwargs: _P.kwargs) Generator[InferenceResult]

Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.

frame() FrameType[source]

The first parent frame node.

A frame node is a Module, FunctionDef, or ClassDef.

Returns:

The first parent frame node.

optional_assign: ClassVar[bool] = True

Whether this node optionally assigns a variable.

Since NamedExpr are not always called they do not always assign.

postinit(target: NodeNG, value: NodeNG) None[source]
scope() LocalsDictNodeNG[source]

The first parent node defining a new scope. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.

Returns:

The first parent scope node.

set_local(name: str, stmt: NodeNG) None[source]

Define that the given name is declared in the given statement node. NamedExpr’s in Arguments, Keyword or Comprehension are evaluated in their parent’s parent scope. So we add to their frame’s locals.

See also

scope()

Parameters:
  • name – The name that is being defined.

  • stmt – The statement that defines the given name.

target: NodeNG

The assignment target

Type:

Name

value: NodeNG

The value that gets assigned in the expression