[Python-ideas] Transportable indent level markers. /// ; ; ; \\\
Massimo Di Pierro
massimo.dipierro at gmail.com
Mon Dec 12 23:40:31 CET 2011
I do not like it and I am not convinced this belongs to Python
Yet you may like this other solution:
exec(reindent("def f(a):; x=0; for i in range(a):; x+=i; pass; return x; print f(5)"))
here reindent is a modification of the web2py reindent function (used in templates).
- ; is converted to \n
- loops are closed with pass
- ifs are also closed with pass
- def can be closed with pass or return
- class must also end with pass
In web2py-ese templates we would not use it like this. we would write instead:
{{def f(a):}}{{x=0}}{{for i in range(a):}}{{x+=i}}{{pass}}{{return x}}{{=f(5)}}
or break it into
{{
def f(a):
x=0
for i in range(a):
x+=i
pass
return x
= f(5)
}} (indentation is irrelevant even within the {{code}})
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reindent.py
Type: text/x-python-script
Size: 4046 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111212/96a2c8d0/attachment.bin>
-------------- next part --------------
On Dec 12, 2011, at 4:07 PM, Ron Adam wrote:
>
> 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
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
More information about the Python-ideas
mailing list