[Python-ideas] Transportable indent level markers. /// ;;; \\\

Ron Adam ron3200 at gmail.com
Mon Dec 12 23:07:13 CET 2011


While typing this reply to the "Alternate Suite Delineation Syntax For
Python?" thread.  I realized that having things  all on a single line
isn't important or needed!!!  Yes, it's not needed for templates, or
code generation.  So this is an alternate to the alternate.

What is needed is a uniform way to represent indenting that is easy to
transport to (ie paste into) code with different indent levels.


INDENT LEVEL MARKERS:
----------------------
Offer a new way to spell <indent+1>, <indent+0>, and <indent-1>.  

To make that work, we need to use unique character sequences that aren't
already used.  After some thought, I came up with these indent-markers.

    ///   # indent level +1
    ;;;   # indent level +0
    \\\   # indent level -1

These are unique enough to be easy to see, and the tripled versions of
them aren't really that bad as we already have triple quotes, and they
would be less than the white space they are replacing in most cases.

So this isn't brackets, or braces. It would be just an alternate
spelling in already valid python. It should not effect the parser,
grammar, or interpreter in any way if it is done in pythons
tokenizer.  :-)


Reusing some previous examples.

#-----------------------

>    def acc(n) {: s=n; return {: def (i) {: nonlocal s; s += i; return
>  s}}}

#Standard python...

def acc(n):
    s=n
    def add(i):
        nonlocal s
        s += i
        return s
    return add


#Single line version...

  def acc(n): /// s=n ;;; def add(i): /// nonlocal s \ 
  ;;; s+=i ;;; return s \\\ return add

(The '\' character should still work.)


#Transportable indent level version...

This could also be valid... and could be transportable to any other
indent level without having to edit it.

(So would the above single line version by adding a ';;;' to the
beginning.)

;;; def acc(n):     # indent +0
/// s = n           # indent +1
;;; def add(i):     # indent +0
/// nonlocal s      # indent +1
;;; s += i          # indent +0
;;; return s        # indent +0
\\\ return add      # indent -1

It has the nice properties of being uniform, and being able to be
inserted easily into any existing code block without having to adjust
the indent levels.

I believe that these properties are more important than being able to
put things on a single line and could be a very useful in code
generating programs. ;-)


It may be easy to implement.

This could probably all be done in the tokinze.c get_tok() function,
where it detects and converts white space to indention levels.  Nothing
else in python would need to be changed.  The tokenizer already detects
line continuations and different line endings, so this wouldn't be that
out of place.

Cheers,
   Ron





More information about the Python-ideas mailing list