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.
atexit module may help. write a cleanup function in your Foo class and register it by calling
atexit.register(function, args)
to be called before your program exits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With