A syntax idea for one-liners

Beni Cherniavsky cben at techunix.technion.ac.il
Sun May 25 16:04:31 EDT 2003


I've recently discovered a backward-compatible and relatively simple
and elegant syntax for encoding arbitrary sets of python statements
with different indentations in a single line.

A single semicolon separates statements at the innermost level.  So
let's separate at outer levels with several semicolons.  E.g.::

    if a:
        if b:
            c
        if d:
            e
    f
    h

could be written as::

    if a: if b: c;; if d: e;;; f; h

I.e. think of the first semicolon as a newline token and the following
ones as dedent tokens.  This is completely backward-compatible since
multiple semicolons currently raise a SyntaxError.

Of course, this should be strongly discouraged in normal code where
one can do otherwise.  However, in certain contexts -- the ``-c``
option and the comman line -- the ability to write one-liners can be
convenient.

Should I write a PEP?  Even if this is rejected outright, it seems
like a nice "discovery" that should be documented for benefit of
designers of new indentation-sensitive langagues that are inspired by
Python...  E.g. I periodically toy with the idea of creating a
Python-based shell; a shell that doesn't allow arbitrary one-liners
would be severly broken.

-- 
Beni Cherniavsky <cben at users.sf.net>

"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis





More information about the Python-list mailing list