Call a Python file with parameters

martin z pxtl at hotmail.com
Tue Jun 10 04:28:18 CEST 2003


>     def function(a, b):
>         '''This function exists to confuse you.'''
>         print 'A is', b
>         print 'B is', a
>
>     import sys
>     sys.modules['file'] = function
>     del sys

This is, of course, the obvious way.  Another way is the from statement.

The problem is that this is clumsy and not good.  I was hoping for a more
streamlined approach to the concept.  Some way to make some sort of "module
header" where the module itself takes the parameters.

>   So, we see that it is possible.  Is it desirable?  Only if you wish to
> confuse readers of your code (including, possibly, yourself) and write
code
> that is of generally poor quality due to its pointless use of wierd
magicks.

Of course, that's why I was asking - I was hoping for a better approach.

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.

Martin.






More information about the Python-list mailing list