No video

Understanding "with" and Python's context managers

  Рет қаралды 901

Python and Pandas with Reuven Lerner

Python and Pandas with Reuven Lerner

9 ай бұрын

Why do we use "with" when opening files? Because "with" invokes the context manager protocol, ensuring that the file is flushed and closed at the end of the block. In this video, I explain what that means, and then show you how the protocol works, and how we can add it to our own classes.

Пікірлер: 12
@sankhasubhrachatterjee5069
@sankhasubhrachatterjee5069 9 ай бұрын
Excellent explanation Sir. Thanks for this knowledge.
@ReuvenLerner
@ReuvenLerner 9 ай бұрын
Glad it helped!
@moose304
@moose304 9 ай бұрын
I've always used the "with" context manager, but mostly because it's "convention" (plus not having to remember to close my file!, or waiting on it to close) I had no idea all the other details. This is great! Thanks Reuven!
@ReuvenLerner
@ReuvenLerner 9 ай бұрын
My pleasure -- glad to know it helped!
@retrain35yo87
@retrain35yo87 3 ай бұрын
good lesson
@ReuvenLerner
@ReuvenLerner 3 ай бұрын
Glad you enjoyed it!
@Doggy_Styles_Coding
@Doggy_Styles_Coding 9 ай бұрын
Why do programmers like dark mode? Because light attracts bugs
@ReuvenLerner
@ReuvenLerner 9 ай бұрын
That's pretty good!
@mansouralshamri1387
@mansouralshamri1387 3 ай бұрын
I don't understand the trapping exceptions in the __exit__ part. Why did you return True?
@ReuvenLerner
@ReuvenLerner 3 ай бұрын
You return True from `__exit__` to ensure that the exception isn't re-raised.
@waynewatson7970
@waynewatson7970 7 ай бұрын
I ran the 2 cells in jupyter lab, and it created and wrote the file so quick, I could see all the lines in the file, not sure how yours was delayed?? on windows, running python3.9 Cell1 # I want to write to a file # I can use the "open" to open a file for writing (with the 'w' option) # help(open) f = open('myfile.txt', 'w') f.write('abcde ') f.write('fghij ') f.write('klmnop ') Cell2 # use the Unix "cat" command to view the file !cat myfile.txt Output right away abcde fghij klmnop thanks. Never mind, it seems to be matching what you had now, so much to learn, thanks for always sharing your knowledge.
@ReuvenLerner
@ReuvenLerner 7 ай бұрын
I'm glad it's working! Note that if/when data is stored to a file is very much dependent on lots of factors. Only by flushing the buffer (or closing the file, which does the same thing, or using "with", which flushes + closes) can you be sure that the file was written to. But if you don't, then it might work just fine... you just can't be sure. Thanks so much for writing and telling me it worked.
Sorting a mix of letters and numbers in Python
9:49
Python and Pandas with Reuven Lerner
Рет қаралды 693
Selecting rows in Pandas using .loc and lambda
9:04
Python and Pandas with Reuven Lerner
Рет қаралды 996
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 100 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 14 МЛН
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 17 МЛН
Five mistakes companies make teaching Python to their staff
8:45
Python and Pandas with Reuven Lerner
Рет қаралды 732
Advanced Golang: Channels, Context and Interfaces Explained
22:17
Code With Ryan
Рет қаралды 116 М.
Rust Demystified 🪄 Simplifying The Toughest Parts
14:05
Code to the Moon
Рет қаралды 178 М.
Method chaining in Pandas
18:17
Python and Pandas with Reuven Lerner
Рет қаралды 1,9 М.
Building A Custom Context Manager In Python: A Closer Look
14:39
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 146 М.
Modern Python logging
21:32
mCoding
Рет қаралды 175 М.
Rust and RAII Memory Management - Computerphile
24:22
Computerphile
Рет қаралды 223 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 100 МЛН