2

The following code comes from Ply, python’s lexer and parser. I understand the first line is a raw string but I also feel that the first line of the code looks like dead code and will be discarded in execution. How could I understand that line of code?

def t_newline(t):
    r'\n+'
    t.lexer.lineno += t.value.count("\n")
wim
  • 338,267
  • 99
  • 616
  • 750
zell
  • 9,830
  • 10
  • 62
  • 115
  • 1
    I think your understanding is correct, but if you're in doubt, you could try deleting it and seeing if the code behaves any differently. – Samwise Mar 07 '23 at 03:53

2 Answers2

4

It's actually a docstring, so that's not "dead code".

>>> def t_newline(t):
...     r'\n'
...     t.lexer.lineno += 1
... 
>>> t_newline.__doc__
'\\n'

ply.lex consumes them.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Do you mean that the lexical analysis will still work even if we remove it? – zell Mar 07 '23 at 04:23
  • 3
    No. That's the opposite of what I mean. The regex for a t_rule is collected from this docstring attribute, and it is [an error condition](https://github.com/dabeaz/ply/blob/66369a66fa85981ab7a5e1dffd4ff7109bf4fa54/src/ply/lex.py#L609-L612) if that's not present. – wim Mar 07 '23 at 04:45
1

That line of code is actually documentation for the function (although its meaning isn't too clear). It populates the .__doc__ property of the function itself. Perhaps the .__doc__ property is used somewhere else in the code.

If you write:

def myFunc():
    'This is my function, it always returns 3'
    return 3

you will get:

print(myFunc.__doc__)

This is my function, it always returns 3
Alain T.
  • 40,517
  • 4
  • 31
  • 51