:mod:`!token` --- Constants used with Python parse trees
Python 3.14 Docs · نسخهٔ 3.14.10 · PSF License
:mod:!token --- Constants used with Python parse trees
:synopsis: Constants representing terminal nodes of the parse tree.
Source code: Lib/token.py
--------------
This module provides constants which represent the numeric values of leaf nodes of the parse tree (terminal tokens). Refer to the file Grammar/Tokens in the Python distribution for the definitions of the names in the context of the language grammar. The specific numeric values which the names map to may change between Python versions.
The module also provides a mapping from numeric codes to names and some functions. The functions mirror definitions in the Python C header files.
Note that a token's value may depend on tokenizer options. For example, a `"+" token may be reported as either PLUS or OP, or a "match" token may be either NAME or SOFT_KEYWORD`.
Dictionary mapping the numeric values of the constants defined in this module back to name strings, allowing more human-readable representation of parse trees to be generated.
Return `True` for terminal token values.
Return `True` for non-terminal token values.
Return `True` if *x* is the marker indicating the end of input.
The token constants are:
Token value that indicates an identifier or keyword <identifiers>.
Token value that indicates a numeric literal <numbers>
Token value that indicates a string or byte literal <strings>, excluding formatted string literals <f-strings>. The token string is not interpreted: it includes the surrounding quotation marks and the prefix (if given); backslashes are included literally, without processing escape sequences.
A generic token value that indicates an operator <operators> or delimiter <delimiters>.
This value is only reported by the tokenize module. Internally, the tokenizer uses exact token types <token_operators_delimiters> instead.
Token value used to indicate a comment. The parser ignores !COMMENT tokens.
Token value that indicates the end of a logical line <logical-lines>.
Token value used to indicate a non-terminating newline. !NL tokens are generated when a logical line of code is continued over multiple physical lines. The parser ignores !NL tokens.
Token value used at the beginning of a logical line <logical-lines> to indicate the start of an indented block <indentation>.
Token value used at the beginning of a logical line <logical-lines> to indicate the end of an indented block <indentation>.
Token value used to indicate the beginning of an f-string literal <f-strings>.
The token string includes the prefix and the opening quote(s), but none of the contents of the literal.
Token value used for literal text inside an f-string literal <f-strings>, including format specifications.
Replacement fields (that is, the non-literal parts of f-strings) use the same tokens as other expressions, and are delimited by LBRACE, RBRACE, EXCLAMATION and COLON tokens.
Token value used to indicate the end of a f-string <f-strings>.
The token string contains the closing quote(s).
Token value used to indicate the beginning of a template string literal.
The token string includes the prefix and the opening quote(s), but none of the contents of the literal.
Token value used for literal text inside a template string literal including format specifications.
Replacement fields (that is, the non-literal parts of t-strings) use the same tokens as other expressions, and are delimited by LBRACE, RBRACE, EXCLAMATION and COLON tokens.
Token value used to indicate the end of a template string literal.
The token string contains the closing quote(s).
Token value that indicates the end of input. Used in top-level grammar rules <top-level>.
Token value that indicates the encoding used to decode the source bytes into text. The first token returned by tokenize.tokenize will always be an `ENCODING` token.
This token type isn't used by the C tokenizer but is needed for the tokenize module.
The following token types are not produced by the tokenize module, and are defined for special uses in the tokenizer or parser:
Token value indicating that a `type: ignore comment was recognized. Such tokens are produced instead of regular COMMENT tokens only with the ~ast.PyCF_TYPE_COMMENTS` flag.
Token value indicating that a type comment was recognized. Such tokens are produced instead of regular COMMENT tokens only with the ~ast.PyCF_TYPE_COMMENTS flag.
Token value indicating a soft keyword <soft-keywords>.
The tokenizer never produces this value. To check for a soft keyword, pass a NAME token's string to keyword.issoftkeyword.
Token value used to indicate wrong input.
The tokenize module generally indicates errors by raising exceptions instead of emitting this token. It can also emit tokens such as OP or NAME with strings that are later rejected by the parser.
The remaining tokens represent specific operators <operators> and delimiters <delimiters>. (The tokenize module reports these as OP; see `exact_type in the tokenize` documentation for details.)
The following non-token constants are provided:
The number of token types defined in this module.
reading the source
A dictionary mapping the string representation of a token to its numeric code.
Added !AWAIT and !ASYNC tokens.
Added COMMENT, NL and ENCODING tokens.
Removed !AWAIT and !ASYNC tokens. "async" and "await" are now tokenized as NAME tokens.
Added TYPE_COMMENT, TYPE_IGNORE, COLONEQUAL. Added !AWAIT and !ASYNC tokens back (they're needed to support parsing older Python versions for ast.parse with `feature_version` set to 6 or lower).
Added EXCLAMATION.
Removed !AWAIT and !ASYNC tokens again.