[Python-ideas] Make keywords KEYwords only in places they would have syntactical meaning
Rob Cliffe
rob.cliffe at btinternet.com
Sat May 19 16:59:18 EDT 2018
On 18/05/2018 12:22, Ken Hilton wrote:
> Hi all,
>
> Yes, this is another idea for avoiding breaking existing code when
> introducing new keywords. I'm not sure if this is too similar to
> Guido's previous "allow keywords in certain places" idea, but here goes:
>
> Only treat keywords as having any special meaning when they are in
> places with syntactical significance.
> So, currently, let's say someone set the variable "and_" to some
> value. The following lines are both SyntaxErrors:
>
> True and_ False
> obj.and = value
>
> And the following are both correct:
>
> True and False
> obj.and_ = value
>
> My idea is to only treat keywords as having special meaning when
> they're in the right place. So the following would all be legal:
>
> >>> from operator import and
> >>> var = and(True, False)
> >>> var
> False
> >>> var = True and False
> >>> var
> False
> >>> def except(exc, def):
> ... try:
> ... return def()
> ... except exc as e:
> ... return e
> ...
> >>> except(ZeroDivisionError, lambda: 1/0)
> ZeroDivisionError('division by zero',)
> >>> except(ZeroDivisionError, lambda: 0/1)
> 0.0
> >>> import asyncio as await #this is already currently legal, but
> will not be in the __future__
> >>> async def async(def):
> ... return await await.get_event_loop().run_in_executor(None, def)
> ...
> >>>
>
> And so on.
>
> What are your thoughts?
>
> Sharing,
>
> Ken Hilton
> ;
>
Great idea, but why restrict it to keywords? Why not also allow
operators to be variable names:
>>> + = 23
>>> + + + + +
69
>>> - = 21
>>> - -
>>> -21
There would be some minor ambiguities, e.g. would
- - - -
evaluate to
(- -) - (-) # -42
or
(-) - (- -) # +42
or
- ( - ( - ( - ))) # -21
or
- (- - -) # 0
and indeed
+ + + + +
could *in theory* evaluate to
+ ( + ( + ( + ( +)))) # 23
or
(++) + (++) # 46
but I'm sure we could formulate some sensible rules to always get the
intended meaning. :-)
Rob Cliffe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180519/8d144345/attachment.html>
More information about the Python-ideas
mailing list