Thoughts on new grammar rules (PEP 284 in particular)

Steve Horne steve at lurking.demon.co.uk
Fri May 10 13:17:36 EDT 2002


I was just reading PEP284 and, in general, I like it. The trouble is,
I'd quite like these limit-specifications to be more general. For
example, I'd quite like to write...

  if 0 <= x < 10 :
    print "x is in range"

There is a big problem with this, of course - this is already legal
Python, but the semantics aren't what I intend.

Looking generally through other PEPs, there seems to be a general
problem with adding new grammar to the Python language. Some
contortions are needed to avoid breaking old code.

I was wondering if maybe it is time that the Python 'core' grammar was
considered broadly complete, with the exception of a special notation
specifically designed for the purpose of wrapping new grammar rules.

I haven't thought through my choice of symbols, but imagine a notation
such as the following was created...

  [* indentifier RULE *]

That is, use delimiters with and enclosed identifier and rule where
the identifier explicitly identifies the grammar rule being used.

We could then have...

range checking...

  if [* check 0 <= x < 10 *] :
    print "OK"

for loop...

  for [* range 0 <= x < 10 *] :
    pass

Obvious Advantages...

- If the new syntax turns out to be obscure and rarely used, people
  reading the code that does use it at least have a clear identifier
  name to look up in the manuals to find an explanation of what it
  does.

- With careful choice of delimiters (ie the [* and *] above) this
  should never cause a code break, and should save a lot of hassle
  in this respect in the future.

- Python should be able to give a clear error message if source code
  intended for a newer version is used (e.g. 'range support not
  provided in this Python version) by simply picking up the
  identifier.

Obvious disadvantages...

- Noticably wordier than necessary, as each new grammar rule has
  three 'redundant' tokens.

- Could make it too convenient to add obscure special-purpose
  features, leading to unnecessary bloat.

Any comments?

-- 
Steve Horne
steve at lurking.demon.co.uk



More information about the Python-list mailing list