CONTEXT MANAGERS In Python Are GENIUS!

Sdílet
Vložit
  • čas přidán 26. 10. 2022
  • What are context managers in Python? How can they give us more flexibility in our code? Let's find out together in this lesson!
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels

Komentáře • 28

  • @wiseskeshom4673
    @wiseskeshom4673 Před rokem +1

    Thank you so much for this informative stuff.

  • @daviddanielng
    @daviddanielng Před 23 dny

    Thanks, i have been thinking about writing my own context manager, but don't have the time to check the docs.
    Thanks.

  • @enesguler6034
    @enesguler6034 Před rokem +2

    I think it is a perfect feature. It may be useful for opening a video in OpenCV. Because I generally forget to close the video stream.

  • @ntlake
    @ntlake Před rokem +3

    You can also use the @contextmanager decorator:
    from contextlib import contextmanager
    class File:
    def __init__(self, path: str, mode: str):
    self.path = path
    self.mode = mode
    self.is_open = True
    def do_something(self):
    if self.is_open:
    print(f"Hi! This is a file. Its path is '{self.path}' and its mode is '{self.mode}'")
    return
    raise ValueError("this file is closed")
    def close(self):
    self.is_open = False
    @contextmanager
    def open_file(path: str, mode: str = "r"):
    file = File(path, mode)
    yield file
    file.close()
    with open_file(r"myfile.txt") as f:
    f.do_something()

  • @castlecodersltd
    @castlecodersltd Před rokem

    A good explanation, thank you

  • @martinking4615
    @martinking4615 Před 18 dny

    You are the man!!

  • @davigalilei9749
    @davigalilei9749 Před 9 měsíci

    Greatly explained.

  • @ConstantlyDamaged
    @ConstantlyDamaged Před rokem

    Any chance of an extra one on this showing operation of "async with"?

  • @mufasaruleroftheanimalking1026

    That‘s another way of wrapping

  • @rantalbott6963
    @rantalbott6963 Před rokem +1

    What gets called if you just fall out of the with block without an exception? Is there a "__else__" to make it consistent with loops? 😆

  • @teksimian
    @teksimian Před rokem +7

    python programmer can't find curly brackets 😂

  • @wellygeek
    @wellygeek Před rokem +1

    How do you implement __ENTER__ and __EXIT__ at the method level not class level?

    • @landsgevaer
      @landsgevaer Před měsícem


      As a static class method? You don't.

  • @tamirshalev9019
    @tamirshalev9019 Před rokem

    Thanks for the explanation! However, 'open' is a function rather than a class, so how does that work actually behins the scenes?

    • @Skrattoune
      @Skrattoune Před 3 měsíci +2

      Obviously it returns an object that has the enter and exit Dunder methods. Probably a FILE object...

  • @RedShipsofSpainAgain
    @RedShipsofSpainAgain Před rokem

    2:04 how did you get that entire line of text to appear automatically? "def __exit__(self, exc_type, exc_val, exc_tb):"
    Is it possible to do this in VS Code?

    • @dopaminedetox8437
      @dopaminedetox8437 Před 9 měsíci

      for me the same, does not show __enter__ and __exit__

    • @SunroseStudios
      @SunroseStudios Před 6 měsíci

      he's using an IDE.looks like Intellij Idea, maybe? not sure

  • @malkistdev
    @malkistdev Před rokem

    What font do you use?

    • @Indently
      @Indently  Před rokem

      I think it's called Jetbrains Mono

  • @devil_brawl
    @devil_brawl Před rokem

    Which app are you using

    • @Ismail_Dagli
      @Ismail_Dagli Před rokem +1

      I guess you mean the IDE he is using? It's PyCharm.

  • @dcknature
    @dcknature Před rokem +1

    Thank you so much for explaining context managers in Python 👍!
    likes = 63 😉😉

  • @mayorc
    @mayorc Před rokem

    What's the code editor name?

  • @grzegorzryznar5101
    @grzegorzryznar5101 Před rokem

    In a most cases it is easier to achieve with contextmanager decorator from contextlib:
    @contextmanager
    def open(name: str):
    print(f'opening file: {name!r}')
    yield 'return value'
    print(f('closing file: {name!r}') # exit block

  • @santoshkumargouda7795

    Is there a discord channel