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

Steven D'Aprano steve at pearwood.info
Mon May 15 09:00:15 EDT 2017


On Mon, May 15, 2017 at 08:13:48PM +1000, Chris Angelico wrote:
> 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. 

I agree with that. But not necessarily the following:


> 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

That is often the case, but there are times where a condition is clearer 
with a pass statement followed by an else than by reversing the sense of 
the test. Or the pass might just be a place-holder: TDD often means that 
there's code where only one branch of an if works (and it's not 
necessarily the if branch).

if condition:
    pass  # will be fixed in the next iteration of TDD
else:
    code


There's also cases where 

    if x > y:
        pass
    else:
        code

is *not necessarily* the same as 

    if not (x > y):
        code

(x > y) is not always not(x <= y). E.g. sets, and even floats.


> 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;

Bare string literals do sometimes mean comments, and I should hope they 
aren't ignored by the reader!

E.g. bare strings at the start of a module, class or function are 
docstrings, and even in the middle of the module or function, they are 
allowed. Guido has spoken! (Unless he's changed his mind since then :-)

https://twitter.com/gvanrossum/status/112670605505077248




> plus, there are contexts where they are not ignored. 

Oh, and here I was thinking strings were ignored everywhere!

print("hello world")  # does nothing

*wink*

But seriously, of course *expression statements* which are string 
literals are not syntactically comments, but they can be, and are, 
treated as if they were. Just use a bit of common sense.


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.

That's because the statement is an assignment statement, not an 
expression statement:

https://docs.python.org/3/reference/simple_stmts.html#grammar-token-expression_stmt



> 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.

Oh, I see where you are coming from! You have interpreted Hugh as 
suggesting that we remove pass and # comments from the language! I 
interpreted him as suggesting the opposite: that we tighten up the 
grammar to prohibit bare expressions, in order to prevent them from 
being used instead of pass or # comments.



-- 
Steve


More information about the Python-ideas mailing list