Comprehension#

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

Bases: NodeNG

Class representing an ast.comprehension node.

A Comprehension indicates the loop inside any type of comprehension including generator expressions.

>>> import astroid
>>> node = astroid.extract_node('[x for x in some_values]')
>>> list(node.get_children())
[<Name.x l.1 at 0x7f23b2e352b0>, <Comprehension l.1 at 0x7f23b2e35320>]
>>> list(node.get_children())[1].as_string()
'for x in some_values'
assign_type()[source]#

The type of assignment that this node performs.

Returns:

The assignment type.

Return type:

NodeNG

assigned_stmts(**kwargs: _P.kwargs) Generator[InferenceResult, None, None]#

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

get_children()[source]#

Get the child nodes below this node.

ifs: list[NodeNG]#

The contents of any if statements that filter the comprehension.

is_async: bool#

Whether this is an asynchronous comprehension or not.

iter: NodeNG#

What is iterated over by the comprehension.

optional_assign: ClassVar[bool] = True#

Whether this node optionally assigns a variable.

postinit(target: NodeNG, iter: NodeNG, ifs: list[NodeNG], is_async: bool) None[source]#
target: NodeNG#

What is assigned to by the comprehension.