Experimenting with 'end' as a Block Terminator in Python
This technical article explores the feasibility of modifying the CPython interpreter to accept 'end' as a valid marker for ending code blocks, similar to languages like Ruby. The author begins by altering the grammar file, python.gram, to treat 'end' as a synonym for the 'pass' statement. However, upon recompiling the interpreter, the process fails because 'end' is frequently used as a variable name in Python's standard library, specifically causing a syntax error in posixpath.py. Recognizing that making 'end' a reserved keyword would break existing code, the author pivots to an alternative strategy. The proposed solution involves adding 'end' as a built-in constant within the __builtins__ module, equivalent to the ellipsis object. This approach aims to provide an optional end-of-block marker without colliding with common variable names or requiring extensive changes to the language's core grammar. The narrative highlights the complexities of extending Python's syntax while maintaining backward compatibility and avoiding conflicts with established coding patterns.
Wire timeline
Experimenting with 'end' as a Block Terminator in Python
This technical article explores the feasibility of modifying the CPython interpreter to accept 'end' as a valid marker for ending code blocks, similar to languages like Ruby. The author begins by altering the grammar file, python.gram, to treat 'end' as a synonym for the 'pass' statement. However, upon recompiling the interpreter, the process fails because 'end' is frequently used as a variable name in Python's standard library, specifically causing a syntax error in posixpath.py. Recognizing that making 'end' a reserved keyword would break existing code, the author pivots to an alternative strategy. The proposed solution involves adding 'end' as a built-in constant within the __builtins__ module, equivalent to the ellipsis object. This approach aims to provide an optional end-of-block marker without colliding with common variable names or requiring extensive changes to the language's core grammar. The narrative highlights the complexities of extending Python's syntax while maintaining backward compatibility and avoiding conflicts with established coding patterns.
DEV Community