Level 1 : What is the purpose of the __enter__ and __exit__ methods in a Python class?

Author: IndraD | Tags: context-manager | 1859+ Python FAQs | Share
loop | regular-expression | python-syntax-and-structure | id-function | logical-operator | data-type | implementation | join | name-mangling | import-method | multiple-inheritance | benefit | loops | logical-operator | advanced-file-operations | arithmetic-operation | namedtuple | memory-management | __all__-attribute | mysql | operator-or | data-structure | oop | namespace | integers | print-statement | print | application | lambdas | data-type-conversions | list-mutation | list-indexing | datatype | context-manager | pickling--unpickling | file-handling | max-min | oops | reading-and-writing-files | type-conversion-and-casting | matplotlib | list-comprehension | handling-missing-values-(fillna,-dropna) | mathematical-function | methods | list-comprehensions | directory-operations | managing-directories-with-os-and-shutil | random | exponentiation | filtering-and-indexing | method | constructor | identifier | identity-operator | thread | random-function | aggregating-data-with-functools.reduce() | function-argument | choice | why-use-multiprocessing-over-threading?-(gil) | using-else-and-finally | call | walking-through-directories-using-os.walk() | memory | precedence | variable | matrix-list-comprehension | assignment-operator | logical-operation | working-with-binary-files-(rb,-wb) | identity-operators-(is,-is-not) | filter-function | tuple | while-else-loop | conditional-statement | indexing:-loc-vs-iloc | touples | python-django | multithreading | attribute | list-comprehension | type-conversion | raise | immutable-data-type | nested-loop | data-type | data-types | boolean | pandas | generators-in-python | integer | math | virtual-environment | enumerate() | lambda | destructor | list-method | global-variable | comment | interpreter | thread-synchronization | async | conditional-statements | using-yield-in-functions | introduction-to-threads | file | datetime.date,-datetime.time,-datetime.datetime | iterators | file_handling | zip-function | list-methods-and-slicing | split | head-and-tail | quantifiers-(*,-+,-?,-{}) | slice | center | raise-statement | operations | defining-functions | control-flow | control-statement | floating-point-precision | statement | iterable | indexing | class-and-inheritance | map-function | file-handling | append | global-variable | keyword | data-structures-in-python | package-management | operator-overloading | format | list,-set,-and-dictionary-comprehensions | strings | data-structure | escape-operator | class-decorators | sys | inheritance | booleans | dictionary-methods | list-operation | multiple-except-blocks | data_structure | difference-between-threads-and-processes | debugging | bitwise | string-manipulation | using-the-threading-module | unit-testing | syntax | init | deep-copy | encapsulation | assert | arithmetic-expression | mutable | multiprocessing | class-and-object | update | input-function | global-keyword | tuples | parameter | using-map()-for-transformations | syntax-error | numpy | mapping-function | operator-and | frozenset | python | intering | sorting-and-ranking | datetime | list-slicing | string-methods | access-modifier | lambda-function | working-with-temporary-files-using-tempfile | default-mutable-argument | match | syntax-error | global | stack | functions | input | collection | __future__ | logical-operators | with-statement | assignment-operators | goto-function | conditional-statement | reading:-read(),-readline(),-readlines() | python-operators | library | overriding-methods | encoding | numeric | immutability | regular-expressions | basic | overflow | round-function | dictionary-comprehensions | string-formatting | dicts | tuple-methods | string-method | swapping | magic-method | built-in-function | thread-safe-data-structures-(queue.queue) | creating-and-starting-threads | invalid-list | polymorphism | class | identity-operator | using-os-and-pathlib-modules | classes | key-value-pairs | sorted | else-statement | list | range | set-operation | introduction-to-multiprocessing | formal-and-actual-parameter | pass | string-method | bitwise-operator | type_conversion | using-assertions-(assertequal,-assertraises) | operator | string-slicing | decorator | opening-files-(open()) | matrix | literal-characters-and-special-characters | array | working-with-dates-and-times | software-design | design-principle | boolean-and-non-boolean | min-max-function | while-loop | zfill-function | while-and-for-loop | conversion | indentation | built-in-function | lists | condition | factorial-function | using-join()-and-is_alive()-methods | single-inheritance | regex | bitwise-operator | sets | heap-memory | generators | binary-operation | math-copysign | while-loops | command-line-parsing | object-attribute | sum-function | mathematics | compute | fabs-function | built-in-modules | sorting | input-function | docstring | zip | else | __init_subclass__ | version | error-handling | recursion | comprehension | rollback | default-arguments | threading | exception | decimal | counter-function | anchors-(^,-$) | python-history | generator | joining-and-merging-dataframes | min-function | operators | copy | iterator | using-lists-as-stacks | complex-number | postfix | membership-operators-(in,-not-in) | python-module | version-check | string-slicing | control-flow | odd-number | pep | parsing-with-strptime | string-formatting | pseudocode | string | dictionary | binary | reading-and-writing-large-files-efficiently | function-and-scope | exception-handling | set | functional-programming | string-concatenation | type-conversion | shallow-copy | def | list-join | error | collections | creating-custom-modules | scientific-notation | staticmethod | objects | for | descriptors | yield | loop-control-statements | string-iteration | list-vs-tuple | typeerror | virtual-environment | module | dictionaries | asyncio-module | arbitrary-arguments-(*args,-**kwargs) | exception-handling | loop-control | recurssion | filter | operand | lambda-functions | installation | gil | formatting-with-strftime | writing:-write(),-writelines() | dataframe-and-series-objects | asyncio | method-overloading | introduction-to-python | sys-module | expression | expand-tab | test-discovery | data-structures | return-statement | class-and-instance-variable | counter | python-repl | boolean-masking | built-in | syntaxerror | feature | multiple-inheritance | comparison | round | tuple-unpacking | higher-order-function | turtle | working-with-file-paths | function | inherit | functional-programming | floats | error-handling | sum() | time-series-with-pandas | slicing | int | precedence-and-associativity | python_version | static-method | complex ...