Triple quoted string in exec function ?

John Machin sjmachin at lexicon.net
Fri Jan 2 09:18:06 EST 2009


On Jan 3, 12:39 am, Stef Mientki <stef.mien... at gmail.com> wrote:
> Stef Mientki wrote:
> > hello,
>
> > I'm running scripts, with the execute function (Python 2.5),
> > and it seems that triple quoted strings are not allowed.
>
> > Is there a workaround,
> > or is this a fundamental problem of the exec-function ?
>
> > thanks,
> > Stef Mientki
>
> thanks for all the answers,
> but I still don't understand it yet :-(
> So maybe someone could explain why the next few lines of code don't work:

Stef, "it doesn't work" is user-speak. Say what what you expected, and
what happened instead.

> Code = ''
> Code += """multiline comment
> and more lines"""
> exec ( Code )

That is equivalent to:
code = "multiline comment\nand morelines"
exec(code)

which of course produces a Syntax Error. What did you expect?

Oh, yeah, the so-called "multiline comment". I can't imagine why you
want to put comments in code that you're going to exec, but you need
an extra layer of quotes:

Code += '''"""multiline comment
and more lines"""'''







More information about the Python-list mailing list