Passing indented code to compile()

Michael Hudson mwh at python.net
Wed May 7 08:25:41 EDT 2003


"John Wilson" <tug at wilson.co.uk> writes:

[...]
> What I do is to remove the leading and trailing blank lines, check to see if
> the first character of the remaining lines (if any) is whitespace. If it is
> then I prepend the statement "if 1:". I then append a blank line to the
> resulting code block.
> 
> Questions:
> 
> 1/ Will this always work?

I think so, so long as indentation in the block is consistent.

> 2/ Is there a better way of doing this?

There are more aesthetically pleasing ways, probably, but for
simplicity, efficiency and "just working", your method sounds OK.

Cheers,
M.

-- 
  The Oxford Bottled Beer Database heartily disapproves of the 
  excessive consumption of alcohol.  No, really.
                        -- http://www.bottledbeer.co.uk/beergames.html
                           (now sadly gone to the big 404 in the sky)




More information about the Python-list mailing list