pythonplydead-code

How to understand a line of dead code in a python function?


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")

Solution

  • 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.