Call a Python file with parameters

W Isaac Carroll icarroll at pobox.com
Tue Jun 10 05:02:15 EDT 2003


martin z wrote:
 > The reason I ask this is because I'm using Python in a heavily
 > embedded environment in which each function stands alone and each
 > function is interchangeable.  The functions are fetched out of the
 > database as files. These files could be text or compiled PyC
 > (probably will arrive as pair to encourage editing).  Hence, keeping
 > the code atomic at a function level is needed.  The method you
 > describe above is inappropriate because the code must be run once to
 > build the function, and then the function retrieved. What would be
 > ideal would be to run the code once with the parameters.

So you're trying to avoid the overhead of executing a def statement to
build the function? If the files have already been compiled by the time
you retrieve them from the database, the only thing that the import
statement has to do is load the .pyc and bind the name. Were you
assuming that the function would have to be executed twice (once to
create it, once to call it)? I'm pretty sure that's not the case.
Perhaps someone more familiar with Python internals could help out here.

Have you tried doing it the normal way (from foo import bar) to see if
it's fast enough for your purpose? I'm pretty sure that you'd get
acceptable results, and the code would be much easier to understand.
More importantly, I don't think any of the magic that's been suggested
would turn out to be any faster.

TTFN






More information about the Python-list mailing list