With Python 2.2, it was possible to subclass built-in language types. This means that you can get your own file type, the write() method of which returned self instead of anything like the built-in version. This will also allow you to bind a call to the close() method at the end of your single-line file.
class ChainableFile(file): def __init__(self, *args, **kwargs): return file.__init__(self, *args, **kwargs) def write(self, str): file.write(self, str) return self def OpenFile(filename, *args, **kwargs): return ChainableFile(filename, *args, **kwargs) statusfile = 'statusfile.txt' status = 'OK\n' OpenFile(statusfile,'w').write(status).close()
source share