Understanding Python: Context Managers

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • In this video, I go over context managers.
    Included in the lesson is an overview of what context managers are, how to use the magic methods _enter_ and __exit__, as well as using contextlib's contextmanager decorator to create your own context managers.
    As always, if you have any questions or suggestions for future videos, please leave a comment down below.
    Follow me on Twitter: / jakejcallahan
    Source: github.com/Jac...
    Timelapse music: 失望した by Eva
    Link: • EVA - 失望した [Synthwave]...
    Outro music: Elix by Synthness
    Link: • Synthness - Elix ★ No ...

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

  • @yourdadsbestfriend7101
    @yourdadsbestfriend7101 3 года назад

    bro i cannot believe these videos don't get more views... you truly deserve it and i hope it will happen soon

  • @elyezer
    @elyezer 3 года назад +1

    I like before watching because I know good stuff is coming.

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

    Hi Jake, hope you are doing well
    Just quick remainder that we are still waiting video about Pathlib library)
    thanks!)

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

      You are absolutely correct! I currently have 2 more videos to do on concurrency and was going to move to another core subject, but I can do pathlib after. So let's say it will be the 3rd upcoming video in the Understanding series.
      Let me know if there is anything else you'd like me to cover!

  • @elyezer
    @elyezer 3 года назад

    Looking forward to that pathlib video. That is also my favorite way to deal with files and directories.

    • @eli1882
      @eli1882 10 месяцев назад

      Its out!

    • @JakeCallahan
      @JakeCallahan  10 месяцев назад

      You're right @eli1882!
      ruclips.net/video/m2hRcRV-kSE/видео.html

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

    During the first context manager, it enters the exit method but can't unlink the file as it raises a PermissionError.
    I fixed it by adding tf.close() after tf.flush()

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

      That's a good solution. From a design perspective, try adding it just before the unlink in the exit method. Then users on systems similar to yours would have to worry about doing that manually either

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

      @@JakeCallahan So, how come it did run fine on yours but not on mine? I have the exact same code as far as I can see.
      edit: I also cannot add it right before the unlink in the exit method. It sais WindowsPath object has no attribute 'close'

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

      Ahhh it seems like this is a windows thing then. My operating system is Fedora Linux.
      Python does a good job abstracting away many of the differences between the two, but occasionally things like this pop up.

    • @17crescent
      @17crescent 10 месяцев назад

      def __enter__(self):
      self.file.parent.mkdir(parents=True, exist_ok=True)
      if self.file.exists():
      self.file.unlink()
      self.file.touch()
      self.file_handle = self.file.open("w")
      return self.file_handle
      def __exit__(self, *args):
      self.file_handle.close()
      self.file.unlink()

  • @duke007x3
    @duke007x3 3 года назад +1

    thanks for great content
    Can you please make something with pytest?

    • @JakeCallahan
      @JakeCallahan  3 года назад +1

      Absolutely! I plan to do an entire series on using Python for testing and Pytest will be the very first video.

    • @duke007x3
      @duke007x3 3 года назад +1

      @@JakeCallahan wow sounds great
      thanks a lot