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
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
On Tue, May 15, 2018 at 8:41 PM, Steven D'Aprano <st...@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.
>> 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.
>
> I strongly disagree, but can't seem to get anyone to bite.
Sometimes it's like that, and yes it is frustrating.
> We want to be able to introduce a keyword that was formally a name, still
> allow
> it to be used as a name, still allow code that uses it as a keyword to
> interoperate
>
> with code that uses it as a name, without changing the language
> or implementation too much.
>
> Ideally, Python would still not allow the keyword to be used as a name and a
> keyword in the same file??
For me at least, this is a deal breaker. My libraries tend to be single-file packages, so all my code is in one place
-- only being able to use the keyname as one or the other in my multi-thousand line file does me no good.
--
~Ethan~
Thanks for the reply Todd.If `True` was redefined somewhere else, it would still be `True` for you. You could do `from oldlib import True as true` and have `true` equal `np.bool_(1)`. You could reference `oldlib.True` or do `oldlib.function(True=x)` to interact with the name in the old library.
None of this would actually apply to `True`, as it's a reserved word in all versions. The proposal only applies to new keywords that are used as names in other libraries.
There can be 2 escape characters '\' and '.'
Niki
> Not if you need to make changes in the same tens of thousands of lines file.But what has that got to do with the the syntax of the new code? The old code iswhat it is.
I did think after I replied that `True` wasn't actually reserved until more recently, butthe point still stands: You would be able to reference the name *as defined* in anexternal library, and yeah, it could refer to anything, but that's kinda the point. Wehave to assume the library does something sane with the name. We can't preemptan employee sabotaging `True`.
If your position is that Guido shouldn't introduce keywords that are currently used as names at all,
Fortunately we have Unicode bold characters nowadays
𝐢𝐟 if 𝐢𝐧 in:
𝐫𝐞𝐭𝐮𝐫𝐧 returnLook ma! No syntactic ambiguity!
On 5/17/2018 2:41 PM, Neil Girdhar wrote:
> My preference is to do nothing. If you end up making "where" a keyword
> in Python 3.8, numpy will probably:
> * rename their where function to "where_" in 3.8
> * add a where_ alias in Python < 3.8.
>
> And then people will have to fix their code in 3.8 anyway. Only instead
> of learning a new verbatim syntax, they will just add the familiar
> underscore.
I'm not saying this applies to numpy, but one bonus of using \where
would be that existing 3.7 pickles would work in 3.8 (or I think so,
it's all obviously underspecified at this point). With renaming, pickles
would break.
Eric
On 16/05/2018 10:12, Stephan Houben wrote:
> Hi all,
>
> One problem already alluded to with the \identifier syntax is that it
> only works
> if the old Python version is sufficiently recent to understand \.
>
> What about using parentheses to allow a keyword to be used as an
> identifier:
> (where)(x, y)
>
>
I believe this is the first proposal that allows future-proofing of new
code while preserving complete backward compatibility. As far as I
know, ( keyword ) is never legal syntax.
Of course, putting brackets round every occurrence of every identifier
that you think might become an identifier in the next century is a bit
of a chore. There is no perfect solution.
Best wishes
Rob Cliffe
> My preference is to do nothing. If you end up making "where" a keyword in Python 3.8, numpy will probably:
> * rename their where function to "where_" in 3.8> * add a where_ alias in Python < 3.8.
This assumes that every library that defines something named `where` (and every library that references it)are maintained. If the parser just starts treating `where` as a keyword, working code will not parse. Thatmay be acceptable (I don't know what the policy is), but it makes introducing keywords more costly thanan approach that allows old code to continue working.
> I believe this is the first proposal that allows future-proofing of new code while preserving> complete backward compatibility.My proposal removes the need to future proof anything, and only requiressubtle changes to the syntax (nothing visually different). It also preservesperfect backwards compatibility. Just saying :)
Apart from (False), (True) and (None), there's also (yield).
> Of course, putting brackets round every occurrence of every identifier
> that you think might become an identifier in the next century is a bit
> of a chore. There is no perfect solution.
>
On Thu, May 17, 2018 at 11:41:33AM -0700, Neil Girdhar wrote:
> My preference is to do nothing. If you end up making "where" a keyword in
> Python 3.8, numpy will probably:
This is only incidently about "where". I'm hoping that the "where" (or
"given") proposal is rejected. It is so verbose and redundantly
repetitious in the common case that rather than being an improvement
over the status quo, it is worse. But that's by-the-by.
> * rename their where function to "where_" in 3.8
> * add a where_ alias in Python < 3.8.
>
> And then people will have to fix their code in 3.8 anyway. Only instead of
> learning a new verbatim syntax, they will just add the familiar underscore.
You should be thinking forward two or three versions from now, when
\name is the familiar syntax and name_ looks like you started to write
an identifier using the underscore_words_convention but got distracted
halfway through.
Remember that (if approved) verbatim names will not be "that new syntax"
for long. We don't still talk about "that new fangled list comprehension
syntax" or "that new yield keyword". That was the problem with the "old
versus new style classes" terminology: at the point that "new-style
classes" had been around for six releases, approaching a decade, they
weren't new any more.
> One thing that should ideally be done is to improve the SyntaxError
> processing to special case use of keywords in places that identifiers are
> used.
This is worth doing regardless of whether or not we get verbatim strings
or some other alternative. You ought to raise it on the bug tracker.
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
--
---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/r1kFC8mYEKk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
For that reason, I'd like to make a more modest proposal to *only* add a verbatim versions of keywords as necessary, e.g., "\where" or "\given". That way, there will be no temptation to use that syntax in any other place. If a new version of Python comes out with a new keyword, say "abc", then all of the old Python versions can get a minor revision that knows about "\abc". This would ensure that the backslash syntax is only used to avoid collisions with new keywords.
When 3.7 hits end-of-life, the "\given" (or whatever) can be deprecated.
Steven D'Aprano wrote:
It's Python 3.8, and I learn that in 4.0 "spam" is going to become a keyword. I simply take my code and change all the references spam to \spam, and I've future-proofed the code for 4.0 while still keeping compatibility with 3.8 and 3.9.
Okay, maybe it helps a little bit, but not very much. There
will still be a lot of reluctance to add new keywords, because
of the disruption it will cause to existing code
> And the alternative is to replace all occurrences of
> spam with 𝐬𝐩𝐚𝐦 , which has the same effect and also is
> backward-compatible with 3.x for x < 8.
>
> So there is already a kind of solution available, albeit an ugly one.
You are kidding, I hope.
If that works at all, I don't think its something we want to guarantee
will work. And for what it's worth, what I see is eight empty boxes
(missing glyph symbols).
--
Steve
On Thu, May 17, 2018 at 11:02:23PM -0400, Neil Girdhar wrote:
> However, the difference between the backslash syntax and comprehensions and
> generator functions is that comprehensions and generator functions make the
> language more expressive. The backslash is no more expressive than
> trailing underscore. It's no more succinct, and no more clear. Adding it
> to the language constitutes more that the user needs to learn, which makes
> Python slightly less accessible.
On the contrary: it removes a pain point when dealing with external
libraries. No longer will we have to *transform* the name on both input
and output. Instead, we only need to *escape* the name when written as a
literal.
> I don't like multiple ways of doing the same thing.
Ah, like when people want to use "class" as an identifier, and since
they can't, they write:
klass cls Class
and maybe even occasionally class_ :-)
Or they use a synonym:
kind, clade, type (with or without trailing underscore).
I've seen *every one of those choices* in real code. Except "clade", I
just added that one now.
Remind me again what the "one (obvious) way to do it" is?
> When 3.7 hits end-of-life, the "\given" (or whatever) can be deprecated.
Having a white list of "Permitted keywords you may escape" is horrible
enough without baking in a policy of continued code churn by removing
them from the whitelist every few releases.
--
Steve
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
It could be worse. At least 𝐬𝐩𝐚𝐦 ("\U0001d42c\U0001d429\U0001d41a\U0001d426")
is visually distinct with the right font support. You could (ab?)use the Unicode
Tags block (https://en.wikipedia.org/wiki/Tags_(Unicode_block))
and use something like 'spam\U000e0069\U000e0064' (spam<i><d>). (Luckily, that's not
even valid Python, as the tag characters aren't valid for identifiers.)
--
Clint
On Fri, May 18, 2018 at 11:17:13AM +0200, Stephan Houben wrote:
> And the alternative is to replace all occurrences of
> spam with 𝐬𝐩𝐚𝐦 , which has the same effect and also is
> backward-compatible with 3.x for x < 8.
>
> So there is already a kind of solution available, albeit an ugly one.
You are kidding, I hope.
If that works at all, I don't think its something we want to guarantee
will work.
And for what it's worth, what I see is eight empty boxes
(missing glyph symbols).
Earlier you described this suggestion as "a silly joke".
https://mail.python.org/pipermail/python-ideas/2018-May/050861.html
I think you were right then.
> I am merely defending the status quo.
> I demonstrate how the intended behavior can be achieved using features
> available in current Python versions.
Aside from the fact that font, editor and keyboard support for such
non-BMP Unicode characters is very spotty, it isn't the intended
behaviour.
As you point out, the intended behaviour is that obj.𝐢𝐟 and
obj.if ought to be identical. Since the later is a syntax error, so
should be the former.
> It is guaranteed to work by PEP-3131:
> https://www.python.org/dev/peps/pep-3131
>
> "All identifiers are converted into the normal form NFKC while parsing;
> comparison of identifiers is based on NFKC."
>
> NFKC normalization means spam must be considered the same identifier as
> 𝐬𝐩𝐚𝐦 .
It's not the NFKC normalization that I'm questioning. Its the fact that
it is done too late to catch the use of a keyword.
Earlier you described this suggestion as "a silly joke".
https://mail.python.org/pipermail/python-ideas/2018-May/050861.html
I think you were right then.
> I am merely defending the status quo.
> I demonstrate how the intended behavior can be achieved using features
> available in current Python versions.
Aside from the fact that font, editor and keyboard support for such
non-BMP Unicode characters is very spotty, it isn't the intended
behaviour.
As you point out, the intended behaviour is that obj.𝐢𝐟 and
obj.if ought to be identical. Since the later is a syntax error, so
should be the former.
> It is guaranteed to work by PEP-3131:
> https://www.python.org/dev/peps/pep-3131
>
> "All identifiers are converted into the normal form NFKC while parsing;
> comparison of identifiers is based on NFKC."
>
> NFKC normalization means spam must be considered the same identifier as
> 𝐬𝐩𝐚𝐦 .
It's not the NFKC normalization that I'm questioning. Its the fact that
it is done too late to catch the use of a keyword.
I would think that the rule that normalization is restricted to
identifiers says that it needs to happen AFTER keyword identification
(otherwise it would have applied to the keyword). To follow the rules
and flag identifiers that normalize to keywords, either you need to
normalize early and tag text that had been changed by normalization so
keywords could flag errors (but late enough that you don't normalize
inside strings and such), or you need to normalize late (as is done) but
then add a check to see if the text became the same as a keyword.
Seems a shame to go to extra work to flag as an error something that
really could only have been done intentionally, removing a 'feature' to
help with backwards compatibility.
--
Richard Damon
But I like to think that verbatim names could become the one OBVIOUS way
to do it in the future.
To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.