[Python-ideas] Verbatim names (allowing keywords as names)
Franklin? Lee
leewangzhong+python at gmail.com
Tue May 15 21:53:16 EDT 2018
I assume there can't be space between the backslash and the name, to
prevent ambiguity like in the following:
# Is this `foo = not [1]` or `foo = \not[1]`?
foo = (\
not[1])
A sampling of \ in other languages, for consideration:
- Haskell: A lambda. E.g. `\x -> x+1`
- TeX: A command. E.g. `\bold{Some text.}`.
- Matlab: Matrix division operator.
- PHP: Namespace delimiter. Uh.
While LaTeX users have some intersection with Python users (Scipy!), I
think there are enough differences in the languages that this one more
won't hurt.
On Tue, May 15, 2018 at 9:09 PM, Guido van Rossum <guido at python.org> wrote:
> I like it. I much prefer \ to $ since in most languages that use $ that I
> know of (Perl, shell) there's a world of difference between $foo and foo
> whenever they occur (basically they never mean the same thing), whereas at
> least in shell, \foo means the same thing as foo *unless* foo would
> otherwise have a special meaning.
>
> I also recall that in some Fortran dialect I once used, $ was treated as the
> 27th letter of the alphabet, but not in the language standard. See e.g.
> https://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Dollar-Signs.html. Apparently
> it has a similar role in Java
> (https://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name).
>
> On Tue, May 15, 2018 at 8:41 PM, Steven D'Aprano <steve at pearwood.info>
> 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:
>>
>> \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:
>>
>> 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
>>
>>
>> --
>> Steve
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
More information about the Python-ideas
mailing list