[Python-Dev] PEP 340 -- Clayton's keyword?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu May 5 07:12:07 CEST 2005


How about user-defined keywords?

Suppose you could write

   statement opening

   def opening(path, mode):
     f = open(path, mode)
     try:
       yield
     finally:
       close(f)

which would then allow

   opening "myfile", "w" as f:
     do_something_with(f)

The 'statement' statement declares to the parser that an
identifier is to be treated as a keyword introducing a
block statement when it appears as the first token in a
statement.

This would allow keywordless block-statements that look
very similar to built-in statements, without any danger of
forgetting to make a function call, since a call would
be implicit in all such block-statements.

A 'statement' declaration would be needed in all modules
which use the generator, e.g.

   statement opening
   from filestuff import opening

For convenience, this could be abbreviated to

   from filestuff import statement opening

There could also be an abbreviation

   def statement opening(...):
     ...

for when you're defining and using it in the same module.

Sufficiently smart editors would understand the 'statement'
declarations and highlight accordingly, making these
user-defined statements look even more like the native
ones.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+


More information about the Python-Dev mailing list