I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue.
The situation is this: I am writing fairly large console scripts in Python. They have quite a few command-line options, which lead to configuration variables that are needed all over the program (e.g. the "--verbose" option alone is used by just about every function and method). I am using optparse to handle the actual option parsing, so I have a main function that contains: opts,args = parser.parse_args() My question in a nutshell is: what is the best way to make the parsed options in 'opts' easily available to the rest of my program? - Having 'opts' trickle down by passing it as an explicit parameter is the most flexible option. It allows me to easily separate the options from the command-line parsing -- if I want to call my script interactively or from a GUI instead, I can just craft my own 'opts' object and pass that. But on the other hand, I feel that the proliferation of 'opts' parameters gets ugly, and does not do wonders for the readability of the program. - So especially for shorter scripts, a solution I've used is to define a global config class, and transfer the options to it: class Config: pass With in main(): for opt in vars(opts): setattr(Config, opt, getattr(opts, opt)) or: Config.cmd_opts = opts Now every method that needs it can just call Config.verbose or Config.cmd_opts.verbose or whatever. A variant I have also used occasionally is to derive Config from a Martelli Borg parent class with shared-state instances, so that every function or class that needs access to the config parameters can just instantiate its own 'local' Config() object. I find this works nicely if the program is spread out over more than one file. - A third option I have seen mentioned (possibly even here on c.l.p.), but have not actually tried yet, is that if you decide to have something 'global' anyway, why not just use a module instead of a class to store the options, so that every file can do "import config" and then say "config.verbose" etc. So, does anybody have any particular positive or negative opinions on any of these approaches, and/or are there perhaps possibilities I have overlooked? Many thanks in advance for your feedback. -- Leo Breebaart <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list