Using switches with exec?

Jp Calderone exarkun at intarweb.us
Mon Jan 12 14:48:57 EST 2004


On Mon, Jan 12, 2004 at 05:07:18PM +0000, Premshree Pillai wrote:
>  --- "Diez B. Roggisch" <nospam-deets at web.de> wrote: >
> > I need to run a Python program dynamically within
> > > another program. I am using exec for the purpose.
> > Is
> > > there a way to pass parameter switches to exec?
> > 
> > You can pass a globals-dictionary to exec, which can
> > hold the switches
> > values. 
> > 
> > Diez
> > -- 
> > http://mail.python.org/mailman/listinfo/python-list
> 
> Could you please elucidate with a sample script.
> Suppose I need to pass the switch "-w" to the
> dynamically loaded code (using eval()/exec()).
> Any help is appreciated.
> 

  Options aren't typically passed to "code".  They're passed to the main
function of a program.  For example:

    def main(argv):
        o = GameOptions()
        o.parse(argv[1:])
        ...

  If your code is structured like this, you can avoid using eval() and exec
altogether and simply import and call the main function:

    from foo import main
    main(['progname', '-w'])

  Hope this helps,

  Jp




More information about the Python-list mailing list