[Python-ideas] PEP 505: None-aware operators
Chris Angelico
rosuav at gmail.com
Wed Jul 25 22:28:08 EDT 2018
On Thu, Jul 26, 2018 at 12:14 PM, David Mertz <mertz at gnosis.cx> wrote:
> So now at least TWO proponents of 505 cannot successfully translate a very
> simple example taken almost directly from the PEP!
>
> Is that REALLY a good argument for it being helpful, and not being a bug
> magnet?!
>
Okay. I'll give you the ACTUAL transformation.
food = spam?.eggs?.bacon
can be rewritten as
_tmp = spam
if _tmp is not None:
_tmp = _tmp.eggs
if _tmp is not None:
_tmp = _tmp.bacon
food = _tmp
Except for the fact that _tmp doesn't exist. Do you see why we accept
"mostly-correct" transformations? It is *actually impossible* to
perfectly represent short-circuiting semantics in Python! And before
you go "well that proves my point, this suggestion is bad", let's
apply the same test to a few other pieces of syntax. Rewrite the
following statements without using the syntactic feature named in the
comment:
# 1) Decorators
@deco
def func():
...
# 2) "yield from"
def chain(*iters):
for iter in iters:
yield from iter
# 3) and the big one: generator expressions
# yes, I'm deliberately using x multiple ways here
def f(x): return x*x
x = range(10)
x = (f(x) for x in x if x % 2)
I can pretty much guarantee you that you'll get these at least
slightly wrong. Even experts will get genexps wrong. The most pedantic
will put caveats on their equivalencies (like where I said "_tmp
doesn't exist"), but chances are you won't even notice the
discrepancies.
So if that makes ?. bad, it makes decorators, yield from, and genexps
far FAR worse. We'd better go back to Python 2.3, before they all
existed.
ChrisA
More information about the Python-ideas
mailing list