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
Пікірлер: 29
@sdmagicАй бұрын
Really useful stuff. Thank you! I immediately implemented it in my database class.
@ntlake Жыл бұрын
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()
@teksimian Жыл бұрын
python programmer can't find curly brackets 😂
@theangriestcrusader Жыл бұрын
i wish we used curlies instead of indentation icl ;_;
@mufasaruleroftheanimalking10265 ай бұрын
That‘s another way of wrapping
@enesguler6034 Жыл бұрын
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.
@daviddanielng3 ай бұрын
Thanks, i have been thinking about writing my own context manager, but don't have the time to check the docs. Thanks.
@rantalbott6963 Жыл бұрын
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? 😆
@wiseskeshom4673 Жыл бұрын
Thank you so much for this informative stuff.
@ConstantlyDamaged Жыл бұрын
Any chance of an extra one on this showing operation of "async with"?
@castlecodersltd Жыл бұрын
A good explanation, thank you
@martinking46153 ай бұрын
You are the man!!
@wellygeek Жыл бұрын
How do you implement __ENTER__ and __EXIT__ at the method level not class level?
@landsgevaer4 ай бұрын
❓ As a static class method? You don't.
@davigalilei974911 ай бұрын
Greatly explained.
@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
@tamirshalev9019 Жыл бұрын
Thanks for the explanation! However, 'open' is a function rather than a class, so how does that work actually behins the scenes?
@Skrattoune6 ай бұрын
Obviously it returns an object that has the enter and exit Dunder methods. Probably a FILE object...
@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 Жыл бұрын
for me the same, does not show __enter__ and __exit__
@SunroseStudios9 ай бұрын
he's using an IDE.looks like Intellij Idea, maybe? not sure
@dcknature Жыл бұрын
Thank you so much for explaining context managers in Python 👍! likes = 63 😉😉
@malkistdev Жыл бұрын
What font do you use?
@Indently Жыл бұрын
I think it's called Jetbrains Mono
@mayorc Жыл бұрын
What's the code editor name?
@Indently Жыл бұрын
PyCharm
@devil_brawl Жыл бұрын
Which app are you using
@Ismail_Dagli Жыл бұрын
I guess you mean the IDE he is using? It's PyCharm.