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.
- Połączenia do bazy danych
- Stream siecowe
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