I have several functions that I want to set the verbosity level of. Currently, I'm handling it like so:
class Foo:
def __init__(self, foo_stuff, verbose=True):
self.print_file = None if verbose else open(os.devnull, 'w')
def do_stuff(self):
print('doing stuff', file=self.print_file)
This works, but I don't like that I never close the file self.print_file.
For cleanliness, I'd prefer not to wrap every single print function in a with open(...). I was wondering if anyone could suggest another way of doing this. For this application, I don't think the python logging module will work.