[Python-ideas] Syntax idea: escaping names to avoid keyword ambiguity
Clint Hepner
clint.hepner at gmail.com
Mon May 14 10:02:34 EDT 2018
> On 2018 May 14 , at 6:47 a, Daniel Moisset <dmoisset at machinalis.com> wrote:
>
> Following up some of the discussions about the problems of adding keywords and Guido's proposal of making tokenization context-dependent, I wanted to propose an alternate way to go around the problem.
My main objection to what follows is that it doesn't seem to offer any benefit over the current practice of appending an underscore (_) to a keyword to make it a valid identifier.
>
> My proposal essentially boils down to:
> • The character "$" can be used as a prefix of identifiers. formally,
> identifier ::= ["$"] xid_start xid_continue*
> • The "$" character is not part of the name. So the program "foo=3;print($foo)" prints 3. So does the program "$foo=3; print(foo)". Both set an entry to globals["foo"] and keep globals["$foo"] unset.
What is the benefit here? ``globals`` returns a mapping that represents the global scope, but that doesn't mean the mapping *is* the global scope. Aside from dubious uses of globals()['class'] to sidestep parsing issues, '$class' would still be, for all practical purposes, the "real" name of the identifier.
> • if "$" appears in a token, it's always an identifier. So "$with", "$if", "$return" are all identifiers.
> If you overcome the "yikes, that looks like awk/bash/perl/php, and I don't like those", and consider it as an escape for "unusual"/"deprecation" situations, I think it's not a bad chose, and allows to a simple solutions to many problems that have been in discussion recently and not so recently. [examples below]
The same argument applies to the underscore (or could, since it would be trivial to promise that no future keyword will end with, or even contain, an underscore).
Defining new *keywords* to take the $-prefix could be done in a backwards-compatible way, although IMO the $ is too ugly to be a realistic choice. There might be a Unicode character that would make a good prefix, but given the reluctance to extend the core grammar beyond 7-bit ASCII, I've haven't spent any time looking for good candidates.
> For me the benefits of this approach are:
> • It's very simple to explain how to use and its semantics
> • It (seems to me it) should be easy to explain to a python apprentice what a "$" means in code they read on a book/blogpost/manual
> • It's very easy to implement, minimal changes in the tokenizer
> • It's also easy to implement/integrate in other tools (editors with syntax highlighters, code formatters, etc)
> • It is easy to see that it's 100% backwards compatible (I understand that "$" has never been used in python before)
The above 5 points all apply to appending an underscore to a keyword to create a valid identifier.
> • It is relatively unsurprising in the sense that other languages are already using $ to label names (there may be some point of confusion to people coming from javascript where "$" is a valid character in names and is not ignored).
Given the variation in how other languages use $, I'm not sure this is a point in favor. There are plenty of questions
on Stack Overflow about how and when to use $ in bash, and much of the confusion appears to stem from how $ is used in Perl.
And that ignores the cases where $ is either optional (arithmetic expressions) or *should not* (the read built-in, the -v conditional operator, etc) be used.
For that matter, why make $ special and restrict it to prefix position, instead of simply allowing $ as a valid identifier character and declaring that no keyword will ever use it?
>
> • It gives python devs and users a clear, easy and universal upgrade path when keywords are added (language designers: Add a __future__ import to enable keyword in python N+1, add warnings to change kw --> $kw in python N+2, and then turn it on by default in python N+3... ; developers: add the import when they want to upgrade , and fix their code with a search&replace when adding the import or after getting a warning).
Nothing about this process is specific to the $-prefix; it applies just as well to the practice of appending _ when it becomes
necessary.
> • It allows you to use new features even if some libraries were written for older python versions, depending the deprecation period (this could be improved with sth I'll write in another email, but that's the topic for another proposal)
I'll withhold judgement on this point, then, but it's not obvious how this allows an old library that uses a newly-minted keyword
as an identifier name to continue working.
> • When clashes occur, which they always do, there's one obvious way to disambiguate (see today the "class_" argument for gettext.translation, the "lambd" argument for random.expovariate, the "class_" filter in libraries like pyquery for CSS class, functions like pyquery, sqlalchemy.sql.operators.as_ , etc. Not counting all the "cls" argument to every classmethod ever)
Any one of these practices could be declared the One True Way without modifying the grammar. You're just adding another one. (https://xkcd.com/927/)
> • If we're worried about over proliferation of "$" in code, I'm quite sure given past experience that just a notice in PEP 8 of "only with $ in names to prevent ambiguity" should be more than enough for the community
And finally, this applies to the trailing underscore as well.
--
Clint
More information about the Python-ideas
mailing list