CONTEXT MANAGERS In Python Are GENIUS!

Поделиться
HTML-код
  • Опубликовано: 26 ноя 2024

Комментарии • 29

  • @sdmagic
    @sdmagic 3 месяца назад

    Really useful stuff. Thank you! I immediately implemented it in my database class.

  • @ntlake
    @ntlake Год назад +6

    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()

  • @enesguler6034
    @enesguler6034 2 года назад +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.

  • @mufasaruleroftheanimalking1026
    @mufasaruleroftheanimalking1026 7 месяцев назад +1

    That‘s another way of wrapping

  • @daviddanielng
    @daviddanielng 5 месяцев назад

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

  • @grzegorzryznar5101
    @grzegorzryznar5101 Год назад

    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

  • @wiseskeshom4673
    @wiseskeshom4673 2 года назад +1

    Thank you so much for this informative stuff.

  • @davigalilei9749
    @davigalilei9749 Год назад

    Greatly explained.

  • @teksimian
    @teksimian 2 года назад +12

    python programmer can't find curly brackets 😂

  • @martinking4615
    @martinking4615 5 месяцев назад

    You are the man!!

  • @castlecodersltd
    @castlecodersltd 2 года назад

    A good explanation, thank you

  • @rantalbott6963
    @rantalbott6963 2 года назад +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? 😆

  • @ConstantlyDamaged
    @ConstantlyDamaged Год назад

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

  • @wellygeek
    @wellygeek 2 года назад +1

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

    • @landsgevaer
      @landsgevaer 6 месяцев назад


      As a static class method? You don't.

  • @tamirshalev9019
    @tamirshalev9019 Год назад

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

    • @Skrattoune
      @Skrattoune 8 месяцев назад +2

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

  • @dcknature
    @dcknature 2 года назад +1

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

  • @RedShipsofSpainAgain
    @RedShipsofSpainAgain Год назад

    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 Год назад

      for me the same, does not show __enter__ and __exit__

    • @SunroseStudios
      @SunroseStudios 11 месяцев назад

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

  • @devil_brawl
    @devil_brawl 2 года назад

    Which app are you using

    • @Ismail_Dagli
      @Ismail_Dagli 2 года назад +1

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

  • @malkistdev
    @malkistdev 2 года назад

    What font do you use?

    • @Indently
      @Indently  2 года назад

      I think it's called Jetbrains Mono

  • @mayorc
    @mayorc 2 года назад

    What's the code editor name?

  • @santoshkumargouda7795
    @santoshkumargouda7795 2 года назад

    Is there a discord channel