I would like to create a trivial one-off Python object to hold some command-line options. I would like to do something like this:
options = ??????
options.VERBOSE = True
options.IGNORE_WARNINGS = False
# Then, elsewhere in the code...
if options.VERBOSE:
...
Of course I could use a dictionary, but options.VERBOSE
is more readable and easier to type than options['VERBOSE']
.
I thought that I should be able to do
options = object()
, since object
is the base type of all class objects and therefore should be something like a class with no attributes. But it doesn't work, because an object created using object()
doesn't have a __dict__
member, and so one cannot add attributes to it:
options.VERBOSE = True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'VERBOSE'
What is the simplest "pythonic" way to create an object that can be used this way, preferably without having to create an extra helper class?