__all__
, __author__
, __version__
, etc. should be placed after the module docstring but before any import statements except from __future__
imports. Python mandates that future-imports must appear in the module before any other code except docstrings:with
statement in Python is used in exception handling to make the code cleaner and much more readable. It simplifies the management of common resources like file streams.file.close()
when using with
statement. The with
statement itself ensures proper acquisition and release of resources. An exception during the file.write()
call in the first implementation can prevent the file from closing properly which may introduce several bugs in the code, i.e. many changes in files do not go into effect until the file is properly closed.with
statement makes the code compact and much more readable. Thus, with
statement helps avoiding bugs and leaks by ensuring that a resource is properly released when the code using the resource is completely executed. The with
statement is popularly used with file streams, as shown above and with Locks, sockets, subprocesses and telnets etc.