[Python-ideas] Literate python?

Terry Reedy tjreedy at udel.edu
Wed Mar 9 03:01:25 CET 2011


On 3/8/2011 5:02 PM, Mike Meyer wrote:
> Wild idea, swiped directly from haskell/ghc:
>
> How about making the python interpreter just a little bit smarter,

It already is ;-)
Though not exactly well known, expression statements that consist of a 
literal (number or string) are ignored -- except for string literals in 
docstring position (and then, they are attached as attributes, rather 
than being in the code object.

def f():
     'doc' # to .__doc__
     1 # ignored
     (1,2,3) # will not be ignored, even though constand and unused
     # comments are ignored
     'same as comment'
     '''
     multiline
     comment
     '''

from dis import dis
dis(f)
 >>>
   4           0 LOAD_CONST               5 ((1, 2, 3))
               3 POP_TOP

  10           4 LOAD_CONST               4 (None)
               7 RETURN_VALUE

> If the first non-white-space character after the shebang line (if
> present) is a backslash, then the compiler ignores lines until it sees
> a line consisting of \begin{code} (which could be the first line),
> then compiles lines until it sees a line consisting of \end{code},
> after which it switches back to searching for \begin{code}.

So this appears unnecessary. Just use quotes.

The main problems is that program editors are generally not smart enough 
to do auto text wrapping within multiline strings.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list