optparse
Andrew Dalke
dalke at dalkescientific.com
Wed May 11 20:14:48 EDT 2005
Steven Bethard wrote:
> Well one reason might be that it's easy to convert from an object's
> attributes to a dict, while it's hard to go the other direction:
...
> py> options['x'], options['y']
> ('spam', 42)
> py> o = ??? # convert to object???
> ...
> py> o.x, o.y
> ('spam', 42)
"hard" == "slightly less easy"?
class Spam:
def __init__(self, d):
self.__dict__.update(d)
then
o = Spam(options)
or use the types module (if you have a classic class)
>>> import types
>>> class Spam: pass
...
>>> o = types.InstanceType(Spam, {"x": 5, "y": 10})
>>> o.x
5
>>>
My guess is the original intent was to make the command-line
parameters act more like regular variables. They are easier
to type (x.abc vs. x["abc"]) and the syntax coloring is different.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list