token
--- 與 Python 剖析樹一起使用的常數¶
原始碼:Lib/token.py
This module provides constants which represent the numeric values of leaf nodesof the parse tree (terminal tokens). Refer to the fileGrammar/Tokens
in the Python distribution for the definitions of the names in the context ofthe language grammar. The specific numeric values which the names map to maychange between Python versions.
The module also provides a mapping from numeric codes to names and somefunctions. 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 eitherPLUS
orOP
, ora"match"
token may be eitherNAME
orSOFT_KEYWORD
.
- token.tok_name¶
Dictionary mapping the numeric values of the constants defined in this moduleback to name strings, allowing more human-readable representation of parse treesto be generated.
- token.ISTERMINAL(x)¶
Return
True
for terminal token values.
- token.ISNONTERMINAL(x)¶
Return
True
for non-terminal token values.
- token.ISEOF(x)¶
Return
True
ifx is the marker indicating the end of input.
The token constants are:
- token.NAME¶
Token value that indicates anidentifier.Note that keywords are also initially tokenized an
NAME
tokens.
- token.NUMBER¶
Token value that indicates anumeric literal
- token.STRING¶
Token value that indicates astring or byte literal,excludingformatted string literals.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.
- token.OP¶
A generic token value that indicates anoperator ordelimiter.
CPython 實作細節: This value is only reported by the
tokenize
module.Internally, the tokenizer usesexact token types instead.
- token.COMMENT¶
Token value used to indicate a comment.The parser ignores
COMMENT
tokens.
- token.NEWLINE¶
Token value that indicates the end of alogical line.
- token.NL¶
Token value used to indicate a non-terminating newline.
NL
tokens are generated when a logical line of code is continuedover multiple physical lines. The parser ignoresNL
tokens.
- token.INDENT¶
Token value used at the beginning of alogical lineto indicate the start of anindented block.
- token.DEDENT¶
Token value used at the beginning of alogical lineto indicate the end of anindented block.
- token.FSTRING_START¶
Token value used to indicate the beginning of anf-string literal.
CPython 實作細節: The token string includes the prefix and the opening quote(s), but noneof the contents of the literal.
- token.FSTRING_MIDDLE¶
Token value used for literal text inside anf-string literal,including format specifications.
CPython 實作細節: Replacement fields (that is, the non-literal parts of f-strings) usethe same tokens as other expressions, and are delimited by
LBRACE
,RBRACE
,EXCLAMATION
andCOLON
tokens.
- token.FSTRING_END¶
Token value used to indicate the end of af-string.
CPython 實作細節: The token string contains the closing quote(s).
- token.ENDMARKER¶
Token value that indicates the end of input.
- token.ENCODING¶
Token value that indicates the encoding used to decode the source bytesinto text. The first token returned by
tokenize.tokenize()
willalways be anENCODING
token.CPython 實作細節: This token type isn't used by the C tokenizer but is needed forthe
tokenize
module.
The following token types are not produced by thetokenize
module,and are defined for special uses in the tokenizer or parser:
- token.TYPE_IGNORE¶
Token value indicating that a
type:ignore
comment was recognized.Such tokens are produced instead of regularCOMMENT
tokens onlywith thePyCF_TYPE_COMMENTS
flag.
- token.TYPE_COMMENT¶
Token value indicating that a type comment was recognized.Such tokens are produced instead of regular
COMMENT
tokens onlywith thePyCF_TYPE_COMMENTS
flag.
- token.SOFT_KEYWORD¶
Token value indicating asoft keyword.
The tokenizer never produces this value.To check for a soft keyword, pass a
NAME
token's string tokeyword.issoftkeyword()
.
- token.ERRORTOKEN¶
Token value used to indicate wrong input.
The
tokenize
module generally indicates errors byraising exceptions instead of emitting this token.It can also emit tokens such asOP
orNAME
with strings thatare later rejected by the parser.
The remaining tokens represent specificoperators anddelimiters.(Thetokenize
module reports these asOP
; seeexact_type
in thetokenize
documentation for details.)
Token | Value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following non-token constants are provided:
- token.N_TOKENS¶
The number of token types defined in this module.
- token.EXACT_TOKEN_TYPES¶
A dictionary mapping the string representation of a token to its numeric code.
在 3.8 版被加入.
在 3.5 版的變更:AddedAWAIT
andASYNC
tokens.
在 3.7 版的變更:RemovedAWAIT
andASYNC
tokens. "async" and "await" arenow tokenized asNAME
tokens.
在 3.8 版的變更:AddedTYPE_COMMENT
,TYPE_IGNORE
,COLONEQUAL
.AddedAWAIT
andASYNC
tokens back (they're neededto support parsing older Python versions forast.parse()
withfeature_version
set to 6 or lower).
在 3.12 版的變更:AddedEXCLAMATION
.
在 3.13 版的變更:RemovedAWAIT
andASYNC
tokens again.