I am watching videos of CS262 on Udacity and I am confused by this kind of procedure definitions:

def t_WORD(token):
    r'[^ <>]+' # any reg. exp. ruleset is placed here
    # ... more processing
    # ... more processing
    return token

This code uses the library ply (.lex)

I have some Python experience and knowledge but I am quite confused here by the first line coming after the procedure definition line.

How is that string (reg. exp. string) is used, interpreted or accessed by the Python interpreter? It's just a string not assigned without a variable pointing to it.

I have done the usual Google and SO search but could not find what it actually is.

Thank you in advance for all the answers and explanations.

有帮助吗?

解决方案

That string is the docstring, accessible as t_WORD.__doc__. The PLY library uses it to assign rules to functions.

In this case, PLY is using docstrings for its own purpose. The string is assigned to the __doc__ attribute, and anyone can read the string. In this case, PLY uses it to construct the parser.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top