Thoughts on new grammar rules (PEP 284 in particular)

Steve Holden sholden at holdenweb.com
Fri May 10 14:12:13 EDT 2002


I'm not sure what you think would be wrong. It's only the syntax/semantics
of the "for" statement this PEP would change. As always, the "if" statement
you quote will work (Python has had chained comparisons since God was a
small girl):

>>> for x in range(-10, 20):   # [-10, -9, ... , 19]
...     if 0 <= x < 10:
...         print x, "is in range"
...
0 is in range
1 is in range
2 is in range
3 is in range
4 is in range
5 is in range
6 is in range
7 is in range
8 is in range
9 is in range
>>>

Hope this makes your weekend brighter!

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------


"Steve Horne" <steve at lurking.demon.co.uk> wrote in message
news:82undu4anb47usgck50ebil2ku1f36173q at 4ax.com...
>
> 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