Skip to content

Instantly share code, notes, and snippets.

@jimathyp
Last active August 25, 2022 23:41
Show Gist options
  • Select an option

  • Save jimathyp/11a8c42d987944f5f24d72129dd40e2b to your computer and use it in GitHub Desktop.

Select an option

Save jimathyp/11a8c42d987944f5f24d72129dd40e2b to your computer and use it in GitHub Desktop.

Revisions

  1. jimathyp revised this gist Aug 25, 2022. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions python-context-managers.md
    Original file line number Diff line number Diff line change
    @@ -4,21 +4,22 @@ Python context managers

    context managers provide - the 'with' construct

    ```
    ```python
    with blah:
    do-something()
    ```

    a context manager (blah) implements __enter__ and __exit__methods
    ```
    ```python
    from contextlib import AbstractContextManager
    ```



    https://docs.python.org/3/library/contextlib.html

    class contextlib.AbstractContextManager

    An abstract base class for classes that implement object.__enter__() and object.__exit__(). A default implementation for object.__enter__() is provided which returns self while object.__exit__() is an abstract method which by default returns None. See also the definition of Context Manager Types.

    New in version 3.6.
  2. jimathyp revised this gist Aug 25, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python-context-managers.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Python context managers
    =======================


    cm - the 'with' construct
    context managers provide - the 'with' construct

    ```
    with blah:
  3. jimathyp renamed this gist Aug 24, 2022. 1 changed file with 0 additions and 0 deletions.
  4. jimathyp revised this gist Aug 24, 2022. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions python-context-managers.rst
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,11 @@ with blah:
    ```

    a context manager (blah) implements __enter__ and __exit__methods
    ```
    from contextlib import AbstractContextManager

    ```



    https://docs.python.org/3/library/contextlib.html
  5. jimathyp revised this gist Aug 24, 2022. 1 changed file with 17 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion python-context-managers.rst
    Original file line number Diff line number Diff line change
    @@ -11,4 +11,20 @@ with blah:

    a context manager (blah) implements __enter__ and __exit__methods

    - https://stackoverflow.com/questions/65865543/how-can-i-tell-if-a-function-can-be-used-in-a-context-manager

    https://docs.python.org/3/library/contextlib.html
    class contextlib.AbstractContextManager
    An abstract base class for classes that implement object.__enter__() and object.__exit__(). A default implementation for object.__enter__() is provided which returns self while object.__exit__() is an abstract method which by default returns None. See also the definition of Context Manager Types.

    New in version 3.6.


    - https://stackoverflow.com/questions/65865543/how-can-i-tell-if-a-function-can-be-used-in-a-context-manager




    abstract base class
    Abstract base classes complement duck-typing by providing a way to define interfaces when other techniques like hasattr() would be clumsy or subtly wrong (for example with magic methods). ABCs introduce virtual subclasses, which are classes that don’t inherit from a class but are still recognized by isinstance() and issubclass(); see the abc module documentation. Python comes with many built-in ABCs for data structures (in the collections.abc module), numbers (in the numbers module), streams (in the io module), import finders and loaders (in the importlib.abc module). You can create your own ABCs with the abc module.


  6. jimathyp created this gist Aug 24, 2022.
    14 changes: 14 additions & 0 deletions python-context-managers.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    Python context managers
    =======================


    cm - the 'with' construct

    ```
    with blah:
    do-something()
    ```

    a context manager (blah) implements __enter__ and __exit__methods

    - https://stackoverflow.com/questions/65865543/how-can-i-tell-if-a-function-can-be-used-in-a-context-manager