Language and Compiler Modules
tokenize
Breaks Python source into lexical tokens including comments and source positions. tokenize consumes a byte-returning readline function; generate_tokens consumes text. Tokenization is useful for source analysis but is not a substitute for grammar validation or safe code execution.
Formal Syntax & Structure
python syntaxpython
import tokenizeReturn Value
module
Module namespace available through import; import is a statement, not a value-returning expression.
Working Example
Example Snippetpython
import tokenizefrom io import BytesIOsource = BytesIO(b"x = 7\n")print([t.string for t in tokenize.tokenize(source.readline) if t.type == tokenize.NAME])