RE: Value of exec statements?

Hammond, Mark (MHammond@jm.cmutual.com.au)
Tue, 07 Feb 95 08:58:00 PST

> I'm working on a project where I want to embed snippets of python code in
> a text document that will be evaluated when the document is displayed. I
> want to give the user more flexibility than is allowed with the eval
> function, but I also want a simple variable reference to return the
> variable's value like python does in interactive mode. Is there any way
> to do this with exec? To be more precise, is there any way to obtain the
> value of last python expression evaluated by exec?

It sounds like you want "eval", rather than "exec".

Although exec can execute a suite of statements, it is usually possible to
encapsulate the statements into a function. This function can be "eval"'d,
executing the statements, and returning the object the function returns.
ie, by wrapping your statement in a function (method, module, etc), you
should be able to have the full power of exec in eval.

Is there something with this approach that makes it unsuitable for your
problem?

Mark (MHammond@cmutual.com.au)