How to use the exec statement
aleax at aleax.it
Sun Oct 6 21:20:28 CEST 2002
> Alex Martelli wrote:
>> If you explain what
>> you're trying to do with exec, it's possible that better
>> suggestions can be advanced regarding how to perform that.
> Thx. I am playing with a very simple application using PyQt.
> The main window has a single central widget which is a
> QSplitter. The QSplitter consists of two parts. One is a
> QTextEdit and the other is simply a QWidget. The user can
> enter text in (or into?) the QTextEdit. At the moment the
> text consists of Python statements, like a = ... or
> dosomething(x,y,z,...). Then, when a toolbar button is
> pressed, the statements should be executed by exec and then
> something is done in the other window.
Well, if your +specs+ include using exec, then there's little
you can do to *avoid* exec.
However, fortunately, no part of the spec states that the
exec MUST be in an existing namespace. Therefore, you
can avoid the worst problems: build an artificial namespace
(i.e., a suitably prepared dictionary) in which these statements
are to execute -- populate it with whatever set of names you
think the user will need (module r_exec is also a possibility).
> will then call another function in the second module. This
> second function will call exec.
> Is this sensible?
It does not much matter what module hosts the function
that calls exec, as long as exec is done in a specific dict
specially prepared for the purpose, without the risk of
accidental (or malicious, but that's harder to stop) overwriting
of names that a module needs to keep using.
More information about the Python-list