How to determine if a line of python code is a continuation of the line above it

Dan Sommers me at privacy.net
Sat Apr 8 15:19:28 EDT 2006


On 8 Apr 2006 11:24:04 -0700,
"Sandra-24" <sandravandale at yahoo.com> wrote:

> I'm not sure how complex this is, I've been brainstorming a little, and
> I've come up with:

["This" meaning how to determine if a line of python code is a
continuation of the line above it.]

> If the previous line ended with a comma or a \ (before an optional
> comment)

A line ending with a comma does *not* indicate a single statement spread
out over two lines:

    a = 1,
    print a,
    a = [ ]

None of those lines is a continuation of the line above it.

> That's easy to cover with a regex

> But that doesn't cover everything ...

I think you'll end up having to parse the code in its entirety to do
this correctly.  Consider triple quoted strings and multiple uses of
parenthesis, both of which can be nested arbitrarily, including inside
each other, and arbitrarily nested delimeters are beyond the ability of
regexen.

Is this merely an academic exercise, or is there a larger purpose for
wanting this information?

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist



More information about the Python-list mailing list