Call a Python file with parameters

Jp Calderone exarkun at intarweb.us
Mon Jun 9 20:11:26 CEST 2003


On Mon, Jun 09, 2003 at 03:10:27PM +0000, martin z wrote:
> I've asked this before, but last time I was too vague.  So, I've got a
> simpler request: I want to write and call Python files as if they were
> functions.  The problem is simple: if you write a Python file with only a
> def-statement, you don't get a file you can run with arguments - you get a
> file that builds a function.  If you use the argc and argv syntax, you get a
> file you can run with arguments, but the arguments are strings.   Basically,
> I want to have a Python file that, when I compile it, I get nothing but a
> function, or something I can use like a function, that I can shove a
> parameter tuple into.  Instead I get a code object (no argument tuple) or a
> module (containing functions).
> 
> Can anyone help?
> 

    --- file.py ---

    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

    --- file.py ---

  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.

  Jp

-- 
"Pascal is Pascal is Pascal is dog meat."
                -- M. Devine and P. Larson, Computer Science 340





More information about the Python-list mailing list