[Python-ideas] Tighten up the formal grammar and parsing a bit?

Chris Angelico rosuav at gmail.com
Mon May 15 06:13:48 EDT 2017


On Mon, May 15, 2017 at 7:38 PM, Hugh Fisher <hugo.fisher at gmail.com> wrote:
> I wrote this little Python program using CPython 3.5.2. It's ...
> interesting ... that we apparently don't need comments or pass
> statements any more. Anyone else think it might be worth tightening up
> the grammar definition and parser a bit?
>

Nope. For starters, you shouldn't be using "pass" statements OR dummy
strings to fill in an if statement's body; you can instead simply
write:

if x <= 0:
    x += 1

Or worst case:

if not (x > 0):
    x += 1

For the rest, all you've shown is that trivial expressions consisting
only of string literals will be ignored in certain contexts. The
trouble is that string literals don't really mean comments, and won't
be ignored by most humans; plus, there are contexts where they are not
ignored. Here, rewrite this without comments:

wrong_answer_messages = [
    "Wrong.",
    "Totally wrong, you moron.",
    "Bob, you idiot, that answer is not right. Cordially, Ted.", # Maize
    "That's as useful as a screen door on a battleship.", # BTTF
    # etc
]

String literals won't work here, and even if they did, they would be
_extremely_ confusing. Comments are semantically distinct.

The 'pass' statement has a very specific meaning and only a few
use-cases. It could often be omitted in favour of something else, but
there's not a lot of value in doing so. Comments have very significant
value and should definitely be kept.

ChrisA


More information about the Python-ideas mailing list