The tokenize module provides a lexical scanner for Python source code, implemented in Python. The scanner in this module returns comments as tokens as well, making it useful for implementing ``pretty-printers,'' including colorizers for on-screen displays.
The primary entry point is a generator:
The generator produces 5-tuples with these members:
the token type;
the token string;
(srow, scol) of ints specifying the
row and column where the token begins in the source;
(erow, ecol) of ints specifying the
row and column where the token ends in the source;
and the line on which the token was found.
The line passed is the logical line;
continuation lines are included.
New in version 2.2.
An older entry point is retained for backward compatibility:
The first parameter, readline, must be a callable object which provides the same interface as the readline() method of built-in file objects (see section 2.3.9). Each call to the function should return one line of input as a string.
The second parameter, tokeneater, must also be a callable object. It is called once for each token, with five arguments, corresponding to the tuples generated by generate_tokens().
All constants from the token module are also exported from tokenize, as are two additional token type values that might be passed to the tokeneater function by tokenize():