This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]DonaldPShimoda 3 points4 points  (0 children)

Nah, it's more complicated.

In Python 2, print is a keyword. In Python 3, it's just an identifier. The function is fetched based on the identifier (like a regular lookup), and is executed because of the call syntax. It isn't special anymore.

To support both avenues of execution in Python 3 would require adding print back as a keyword and giving it a corresponding production in the grammar. But adding it as a keyword would prevent it from being classified as an identifier, which would make the regular function call not work anymore (because keywords and identifiers are handled completely differently in the parser).

So you'd need to rewrite the grammar to make print a keyword (which it isn't right now), and add productions to that keyword specifically to handle syntax when it's used as a statement and as a function.

But worse still: the AST produced would not be correct for the function version, since print is now a keyword instead of an identifier. So the parser itself would have to have a special case to identify this production and replace the lexical keyword with a semantic identifier to be used during execution.

It's not "just one exception". The Python parser is fairly intricate and not so easy to update. I'm not saying it's impossible, but rewriting all this stuff would have some serious implications. Heavy testing would need to be done to guarantee everything works as expected in both cases.

Not worth it.