optparse

Steven Bethard steven.bethard at gmail.com
Wed May 11 16:21:50 EDT 2005


Sébastien Boisgérault wrote:
> Any idea why the 'options' object in
> 
> # optparse stuff
> (options, args) = parser.parse_args()
> 
> is not/couldn't be a real dict ? Or why at least it
> does not support dict's usual methods ?

Well, it's not a real dict because the original API intends it to be 
used as object attributes.  However, if you need a dict, it's pretty 
simple -- use vars() or .__dict__:

py> import optparse
py> p = optparse.OptionParser()
py> p.add_option('-x')
<Option at 0x11a01e8: -x>
py> options, args = p.parse_args(['-x', '0'])
py> options.x
'0'
py> vars(options)
{'x': '0'}
py> options.__dict__
{'x': '0'}

STeVe



More information about the Python-list mailing list