optparse: Why does this syntax work and not another?

Anthony Baxter anthonybaxter at gmail.com
Mon Jul 5 02:49:02 EDT 2004


On Mon, 05 Jul 2004 06:36:46 GMT, washu <me at privacy.net> wrote:
> And everything works.  It will print foo.txt.  Now, instead of doing the
> parser = optparse.OptionParser() substitution, I spell everything out:
> 
> optparse.OptionParser().add_option( . . .)
> (options, args) = optparse.OptionParser().parse_args( . . . )
> And the script stops with an error message of: "no such option: -f"

Because your code is incorrect. The first of these two lines 
creates and OptionParser() instance, and calls 'add_option()' 
on it. It then throws away the newly created instance, as you
didn't assign it to anything.




More information about the Python-list mailing list