3

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.

Phil
  • 13,875
  • 21
  • 81
  • 126
  • 1
    Very closely related: http://stackoverflow.com/questions/12373446/regexp-automatically-runs-on-function-input/12373478#12373478 – mgilson Dec 11 '12 at 15:47

1 Answers1

5

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    You're completely right in your answer (+1). Does this imply that PLY would break if you used `python -OO`? That seems like a pretty flaky program design to me ... – mgilson Dec 11 '12 at 15:45
  • I guess it would. I was never a fan of overloading the meaning of docstrings in this way in any case. – Ned Batchelder Dec 11 '12 at 15:49
  • @mgilson nice observation. If someone cant answer it can you try (unfortunately my PLY installation is not working). _or else i ll ask a question (after waiting for ned/you/phil to answer_ – pranshus Dec 11 '12 at 15:50
  • +1, just checked it out in the interpreter myself - for some reason I assumed docstrings had to be defined with triple quotes; I've never seen somebody use single quotes or realstrings for this... – l4mpi Dec 11 '12 at 15:52
  • @pranshus -- I don't even know what PLY is :-p ... so I won't hazard an attempt ... I was merely just curious as I've heard of it before so it must be at least a moderately big deal and that seems like a questionable design decision. – mgilson Dec 11 '12 at 15:53
  • @mgilson PLY is a lex-yacc type processor . They are pretty big on reg-ex so I am assuming (hoping) it should "not" break. – pranshus Dec 11 '12 at 15:55
  • @l4mpi -- [PEP 257](http://www.python.org/dev/peps/pep-0257/) encourages the use of triple double quotes for docstrings which explains why they are so ubiquitous – mgilson Dec 11 '12 at 15:57
  • Hello everyone. Thanks for your thoughts and comments. Just like @l4mpi I also thought the docstring would consist of triple quotes or better to say, however it might be, its purpose was to explain the procedure for readability. This really seems like terrible design to me as well. – Phil Dec 11 '12 at 15:57