Const

class astroid.nodes.Const(value: ~typing.Any, lineno: int | None = None, col_offset: int | None = None, parent: ~astroid.nodes.node_ng.NodeNG = <__SyntheticRoot.__astroid_synthetic l.0>, kind: str | None = None, *, end_lineno: int | None = None, end_col_offset: int | None = None)[source]

Bases: NoChildrenNode, Instance

Class representing any constant including num, str, bool, None, bytes.

>>> import astroid
>>> node = astroid.extract_node('(5, "This is a string.", True, None, b"bytes")')
>>> node
<Tuple.tuple l.1 at 0x...>
>>> list(node.get_children())
[<Const.int l.1 at 0x...>,
<Const.str l.1 at 0x...>,
<Const.bool l.1 at 0x...>,
<Const.NoneType l.1 at 0x...>,
<Const.bytes l.1 at 0x...>]
bool_value(context: InferenceContext | None = None)[source]

Determine the boolean value of this node.

Returns:

The boolean value of this node.

Return type:

bool or Uninferable

getitem(index, context: InferenceContext | None = None)[source]

Get an item from this node if subscriptable.

Parameters:

index (Const or Slice) – The node to use as a subscript index.

Raises:

AstroidTypeError – When the given index cannot be used as a subscript index, or if this node is not subscriptable.

has_dynamic_getattr() → bool[source]

Check if the node has a custom __getattr__ or __getattribute__.

Returns:

Whether the class has a custom __getattr__ or __getattribute__. For a Const this is always False.

infer_binary_op(**kwargs: _P.kwargs) → Generator[InferenceResult]
infer_unary_op(op)
itered()[source]

An iterator over the elements this node contains.

Returns:

The contents of this node.

Return type:

Iterator[Const]

Raises:

TypeError – If this node does not represent something that is iterable.

kind: str | None

“The string prefix. “u” for u-prefixed strings and None otherwise.

pytype() → str[source]

Get the name of the type that this node represents.

Returns:

The name of the type.

value

The value that the constant represents.