audiolab icon indicating copy to clipboard operation
audiolab copied to clipboard

Make Sndfile a "With Statement Context Manager"?

Open mgeier opened this issue 12 years ago • 0 comments

It would be nice to be able to use Sndfile like this:

from scikits.audiolab import Sndfile
with Sndfile("test.wav") as f:
    # do something with f
    print(f)

# no need to call f.close()!

Of course, contextlib.closing could be used for that, but why not support the with statement directly? And, if I'm not mistaken, implementing this would be straightforward, just include the following within the class definition of Sndfile:

def __enter__(self):
    return self

def __exit__(self, *ignored_args)
    self.close()

mgeier avatar Jul 13 '13 12:07 mgeier