CONTEXT MANAGERS In Python Are GENIUS!

  Рет қаралды 29,242

Indently

Indently

Күн бұрын

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
@sdmagic Ай бұрын
Really useful stuff. Thank you! I immediately implemented it in my database class.
@ntlake
@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
@teksimian Жыл бұрын
python programmer can't find curly brackets 😂
@theangriestcrusader
@theangriestcrusader Жыл бұрын
i wish we used curlies instead of indentation icl ;_;
@mufasaruleroftheanimalking1026
@mufasaruleroftheanimalking1026 5 ай бұрын
That‘s another way of wrapping
@enesguler6034
@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.
@daviddanielng
@daviddanielng 3 ай бұрын
Thanks, i have been thinking about writing my own context manager, but don't have the time to check the docs. Thanks.
@rantalbott6963
@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
@wiseskeshom4673 Жыл бұрын
Thank you so much for this informative stuff.
@ConstantlyDamaged
@ConstantlyDamaged Жыл бұрын
Any chance of an extra one on this showing operation of "async with"?
@castlecodersltd
@castlecodersltd Жыл бұрын
A good explanation, thank you
@martinking4615
@martinking4615 3 ай бұрын
You are the man!!
@wellygeek
@wellygeek Жыл бұрын
How do you implement __ENTER__ and __EXIT__ at the method level not class level?
@landsgevaer
@landsgevaer 4 ай бұрын
❓ As a static class method? You don't.
@davigalilei9749
@davigalilei9749 11 ай бұрын
Greatly explained.
@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
@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 6 ай бұрын
Obviously it returns an object that has the enter and exit Dunder methods. Probably a FILE object...
@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 9 ай бұрын
he's using an IDE.looks like Intellij Idea, maybe? not sure
@dcknature
@dcknature Жыл бұрын
Thank you so much for explaining context managers in Python 👍! likes = 63 😉😉
@malkistdev
@malkistdev Жыл бұрын
What font do you use?
@Indently
@Indently Жыл бұрын
I think it's called Jetbrains Mono
@mayorc
@mayorc Жыл бұрын
What's the code editor name?
@Indently
@Indently Жыл бұрын
PyCharm
@devil_brawl
@devil_brawl Жыл бұрын
Which app are you using
@Ismail_Dagli
@Ismail_Dagli Жыл бұрын
I guess you mean the IDE he is using? It's PyCharm.
@santoshkumargouda7795
@santoshkumargouda7795 Жыл бұрын
Is there a discord channel
HOW You Should Be Using ASSERT In Python To Catch ERRORS
7:37
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 60 М.
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 1,8 МЛН
АЗАРТНИК 4 |СЕЗОН 2 Серия
31:45
Inter Production
Рет қаралды 1,1 МЛН
GIANT Gummy Worm Pt.6 #shorts
00:46
Mr DegrEE
Рет қаралды 79 МЛН
5 Good Python Habits
17:35
Indently
Рет қаралды 537 М.
Building A Custom Context Manager In Python: A Closer Look
14:39
PYTHON : Async Context Managers
7:20
Tech With Machines
Рет қаралды 65
8 things in Python you didn't realize are descriptors
14:21
“typing” is getting deprecated in Python
7:20
Indently
Рет қаралды 66 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
5 Really Cool Python Functions
19:58
Indently
Рет қаралды 62 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 101 М.
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 1,8 МЛН