Triple quoted string in exec function ?

Jean-Paul Calderone exarkun at divmod.com
Tue Dec 30 15:22:07 EST 2008


On Tue, 30 Dec 2008 21:16:39 +0100, Stef Mientki <stef.mientki at gmail.com> wrote:
>ibpet11 at gmail.com wrote:
>>On Dec 30, 2:48 pm, Steve Holden <st... at holdenweb.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 ?
>>>>
>>>If you think about it, it should be obvious that you can't surround a
>>>string to be compiled with any of the quotes that appear inside the
>>>string to be compiled. That's about the only limitation I am aware of.
>>>
>>>And, by the way, exec is a *statement*, not a function!
>>>
>      exec ( Init_Code, PG.P_Globals )
>
>I've really doubt that this is a statement,
>unless I don't understand what a statement is.

What do you think a statement is?  According to the Python grammar, exec
is this:

    exec_stmt: 'exec' expr ['in' test [',' test]]

"stmt" is short for "statement". :)  A more satisfying demonstration of the
statementness of exec is this, though:

    >>> x = exec "foo"
      File "<stdin>", line 1
        x = exec "foo"
               ^
    SyntaxError: invalid syntax
    >>> 

And no, putting parenthesis around the expression given to exec doesn't make
a difference:

    >>> x = exec("foo")
      File "<stdin>", line 1
        x = exec("foo")
               ^
    SyntaxError: invalid syntax
    >>> 

Jean-Paul



More information about the Python-list mailing list