On this page
  1. ast.literal_eval
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

ast.literal_eval

Prev: ast.iter_fields | Up: ast | Next: ast.main
Rel: ast.Add, ast.alias, ast.And, ast.AnnAssign
Ref: docs.python.org — ast.literal_eval
Tags: #public #python3 #stdlib #ast


Import

import ast
from ast import literal_eval

Signature & Overview

ast.literal_eval(node_or_string)

Evaluate an expression node or a string containing only a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts,

Usage

import ast

val = ast.literal_eval("{'a': [1, 2, True, None]}")
assert val == {"a": [1, 2, True, None]}

Gotchas

REPL Inspect

from ast import literal_eval
help(literal_eval)

Linked from

Related notes