6. Context Managers

Local variables in Python have function scope, and thus the target of a with statement, if any, is still visible after the block has exited, though __exit__() has already been called on the context manager (the argument of the with statement), and thus is often not useful or valid.

  • __enter__()
  • __exit__()

6.1. Zastosowanie

  • Połączenia do bazy danych
  • Pliki
  • Stream siecowe

6.2. Przykład

f = open(filename)
# ...
f.close()
f = open(filename)
try:
    # ...
finally:
    f.close()
with open(filename) as f:
    # ...
@contextmanager
def FileName(*args, **kwargs):
   with File(*args, **kwargs) as f:
       yield f.name