[docs] Bug in re example

Herman L. Jackson hjackson at wne.edu
Wed May 11 19:36:26 CEST 2011


When running the tokenize example at the bottom of http://docs.python.org/py3k/library/re.html#match-objects
I received the following error:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 23, in tokenize
    NameError: global name 'val' is not defined

The problem is that val is never assigned to before this line:

    if typ == 'ID' and val in keywords:

Here is a suggested rewrite of the that section:

    elif typ != 'SKIP':
        val = mo.group(typ)
        if typ == 'ID' and val in keywords:
            typ = val
        yield Token(typ, val, line, mo.start()-line_start)

Attached is a complete working example.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tokenize.py
Type: text/x-python-script
Size: 1431 bytes
Desc: tokenize.py
URL: <http://mail.python.org/pipermail/docs/attachments/20110511/986b9061/attachment-0001.bin>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: ATT00001.txt
URL: <http://mail.python.org/pipermail/docs/attachments/20110511/986b9061/attachment-0001.txt>

More information about the docs mailing list