No video

CONTEXT MANAGERS In Python Are GENIUS!

  Рет қаралды 28,028

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 7 күн бұрын
Really useful stuff. Thank you! I immediately implemented it in my database class.
@mufasaruleroftheanimalking1026
@mufasaruleroftheanimalking1026 4 ай бұрын
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.
@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 ;_;
@wiseskeshom4673
@wiseskeshom4673 Жыл бұрын
Thank you so much for this informative stuff.
@daviddanielng
@daviddanielng 2 ай бұрын
Thanks, i have been thinking about writing my own context manager, but don't have the time to check the docs. Thanks.
@martinking4615
@martinking4615 2 ай бұрын
You are the man!!
@davigalilei9749
@davigalilei9749 10 ай бұрын
Greatly explained.
@castlecodersltd
@castlecodersltd Жыл бұрын
A good explanation, thank you
@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? 😆
@ConstantlyDamaged
@ConstantlyDamaged Жыл бұрын
Any chance of an extra one on this showing operation of "async with"?
@wellygeek
@wellygeek Жыл бұрын
How do you implement __ENTER__ and __EXIT__ at the method level not class level?
@landsgevaer
@landsgevaer 3 ай бұрын
❓ 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 5 ай бұрын
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 11 ай бұрын
for me the same, does not show __enter__ and __exit__
@SunroseStudios
@SunroseStudios 8 ай бұрын
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
@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
@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
Рет қаралды 58 М.
English or Spanish 🤣
00:16
GL Show
Рет қаралды 5 МЛН
Get 10 Mega Boxes OR 60 Starr Drops!!
01:39
Brawl Stars
Рет қаралды 18 МЛН
Кадр сыртындағы қызықтар | Келінжан
00:16
The Joker saves Harley Quinn from drowning!#joker  #shorts
00:34
Untitled Joker
Рет қаралды 72 МЛН
Context Managers in Python Make Life Easier
15:01
NeuralNine
Рет қаралды 12 М.
Python Tutorial: Context Managers - Efficiently Managing Resources
20:37
10 Nooby Mistakes Devs Often Make In Python
24:31
Indently
Рет қаралды 59 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 57 М.
Building A Custom Context Manager In Python: A Closer Look
14:39
8 things in Python you didn't realize are descriptors
14:21
5 More Useful F-String Tricks In Python
9:38
Indently
Рет қаралды 47 М.
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 802 М.
5 Useful Python Decorators (ft. Carberra)
14:34
Indently
Рет қаралды 97 М.
English or Spanish 🤣
00:16
GL Show
Рет қаралды 5 МЛН