[Python-ideas] Verbatim names (allowing keywords as names)

Terry Reedy tjreedy at udel.edu
Tue May 15 23:20:34 EDT 2018

On 5/15/2018 8:41 PM, Steven D'Aprano wrote:
> Inspired by Alex Brault's  post:
> https://mail.python.org/pipermail/python-ideas/2018-May/050750.html
> I'd like to suggest we copy C#'s idea of verbatim identifiers, but using
> a backslash rather than @ sign:

Not quite as heavy.

>      \name
> would allow "name" to be used as an identifier, even if it clashes with
> a keyword.
> It would *not* allow the use of characters that aren't valid in
> identifiers, e.g. this is out: \na!me  # still not legal
> See usage #1 here:
> https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

> If "verbatim name" is too long, we could call them "raw names", by
> analogy with raw strings.

> I believe that \ is currently illegal in any Python expression, except
> inside strings and at the very end of the line, so this ought to be
> syntactically unambgiguous.
> We should still include a (mild?) recommendation against using keywords
> unless necessary, and a (strong?) preference for the trailing underscore
> convention. But I think this doesn't look too bad:

I think it is just ugly enough to discourage wild use.

>      of = 'output.txt'
>      \if = 'input.txt'
>      with open(\if, 'r'):
>          with open(of, 'w'):
>              of.write(\if.read())
> maybe even nicer than if_.
> Some examples:
>      result = \except + 1
>      result = something.\except
>      result = \except.\finally

I believe avoiding tagging raw names as keywords could be done by 
adjusting the re for keywords and that addition of '\' could be done by 
re.sub.  (The details should be in the doc.)

Terry Jan Reedy

More information about the Python-ideas mailing list