First page Back Continue Last page Overview Graphics

Context Manager

Context managers are used to allocate and release some sort of resource when you need it.

The best know use case is file access:

with open("test.txt", "w") as fh:

fh.write("Πάλι βρέχει στο Λονδίνο,\n")

fh.write("Κι έχει ζέστη στην Ελλάδα\n")

with open("test.txt") as fh:

for line in fh:

print(line)