ID EN
Contextlib

closing

Python 3.11 🇮🇩 Bahasa Indonesia

Kembalikan manajer konteks yang menutup sesuatu setelah blok selesai. Ini pada dasarnya setara dengan:

Syntax

PYTHON
contextlib.closing(thing)

Contoh

Example 1
PYTHON
from contextlib import contextmanager

@contextmanager
def closing(thing):
    try:
        yield thing
    finally:
        thing.close()
Example 2
PYTHON
from contextlib import closing
from urllib.request import urlopen

with closing(urlopen('https://www.python.org')) as page:
    for line in page:
        print(line)

See Also

with context manager with urlopen()