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

Chris Angelico rosuav at gmail.com
Mon May 15 09:17:48 EDT 2017


On Mon, May 15, 2017 at 11:00 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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.
>
> 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.

Uhm.... not sure what you're getting at here. I'm fully aware that:

if x > y:
    pass
else:
    code

is not the same as:

if x <= y:
    code

but I don't know of any way that it could be different from:

if not (x > y):
    code

because that's going to evaluate (x > y) exactly the same way the
original would, and then perform a boolean negation on it, which is
exactly the same as the if/else will do. Or have I missed something
here?

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

Yes, that was what I was interpreting his statements as. I now know
better, so you can ignore a lot of my comments, which were about that
:)

So. Taking this the other way, that Hugh intended to make dumb code
illegal: I think it's unnecessary, because linters and optimizers are
better for detecting dead code; it's not something that often crops up
as a bug anywhere.

ChrisA


More information about the Python-ideas mailing list