2 new comment-like characters in Python to aid development?

dbhbarton at googlemail.com dbhbarton at googlemail.com
Fri Mar 9 04:14:46 EST 2007


Had a thought that's grown on me. No idea if it's original or not- too
inexperienced in programming- but I guess there's no harm floating it
out there.

Python wins big on readability, and there's no doubt that context-
dependent text formatting in IDEs (keywords, strings, comments etc) is
a massive help too, therefore benefitting development and maintenance.
This idea is in a similar vein, especially for when scripts grow
large.

What if 2 new 'special' comment-like characters were added to Python?:


1. The WIP (Work In Progress) comment:

A '?' placed in the preceding whitespace of a line as a means of
quickly highlighting a line or block of code for special attention.
The interpreter simply ignores these characters, and executes the code
as if each WIP character wasn't there. The value-added comes from how
IDEs can exploit this to color the line or code block (in a
customisable fashion as with other context-dependent IDE formatting).

Thus...
?a=6 #This line gets highlighted.
?class MyClass: #This entire class gets highlighted.
    def __init__(self):
        self.val=3
?def MyFn(): #This entire function gets highlighted.
    return 'x'
?for each in range(9): #This entire block gets highlighted.
    print each

Simply delete the ? and the associated highlighting vanishes
immediately.
Indeed if the interpreter can tolerate one '?' then perhaps it can
also allow '??' and '???', letting the IDE color each differently for
some additional flexibility.

Applications...
Lets you highlight / un-highlight entire blocks with a single
keystroke: to record which part of a long script you're working on, or
which part needs development or optimization. IDEs could add
additional functionality if they chose: options to remove all wip
comments, or step through them, or even to automatically add WIP
comments (to highlight syntax errors, potentially infinite loops, or
rate-limiting code blocks, perhaps?)


2. The HALT comment:

A '!' at the start of a line, indicating the end of the script proper.
The interpreter would register this one, and ignore everything after
it, a bit like a sys.exit() call but also stopping it from picking
syntax errors after the HALT. IDEs could then 'grey out' (or 'yellow
out' or whatever) all following characters, including later HALT
comments.

Applications...
Lets you mask / unmask the tailing parts of a py script with a single
keystroke: potentially quite useful during the writing / testing phase
of coding when you might not want to run the whole thing, or as
another means of adding extensive comments to the end of a file. Could
also be rather handy in 'tutorial scripts' and the like...

E.g...
# Welcome to my Python Tutorial Script
my_string="Hello World"
print my_string
! # Everything after this '!' is ignored and greyed out for now, but
when you're ready to move on to the next part of the tutorial script
just delete the '!' and run it again.
my_list=list(my_string)
print my_list
! # <-- delete '!' when ready, etc etc
my_list_reversed=my_list[::-1]
print my_list_reversed


As far as I can see, neither of these would break backwards
compatibility and, like the @ decorator, if you don't like it, you
wouldn't have to use it. I don't know enough about the guts of Python
to say much about ease of implementation, but it doesn't seem like it
would be too hard.

Personally I'd use these a lot, but I'm a rank amateur so maybe I just
don't develop code properly.
That's it. Useful? Pointless? Dangerous? Stupid?


Dave.




More information about the Python-list mailing list