Тёмный

Understanding "with" and Python's context managers 

Python and Pandas with Reuven Lerner
Подписаться 19 тыс.
Просмотров 770
50% 1

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.

Наука

Опубликовано:

 

5 ноя 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 12   
@sankhasubhrachatterjee5069
@sankhasubhrachatterjee5069 7 месяцев назад
Excellent explanation Sir. Thanks for this knowledge.
@ReuvenLerner
@ReuvenLerner 7 месяцев назад
Glad it helped!
@moose304
@moose304 7 месяцев назад
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 7 месяцев назад
My pleasure -- glad to know it helped!
@retrain35yo87
@retrain35yo87 Месяц назад
good lesson
@ReuvenLerner
@ReuvenLerner Месяц назад
Glad you enjoyed it!
@Doggy_Styles_Coding
@Doggy_Styles_Coding 7 месяцев назад
Why do programmers like dark mode? Because light attracts bugs
@ReuvenLerner
@ReuvenLerner 7 месяцев назад
That's pretty good!
@mansouralshamri1387
@mansouralshamri1387 Месяц назад
I don't understand the trapping exceptions in the __exit__ part. Why did you return True?
@ReuvenLerner
@ReuvenLerner Месяц назад
You return True from `__exit__` to ensure that the exception isn't re-raised.
@waynewatson7970
@waynewatson7970 5 месяцев назад
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 5 месяцев назад
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.
Далее
The six most important read_csv arguments in Pandas
16:50
What is a context manager?
2:36
Просмотров 390
#4 IPython %magic Commands
6:20
Просмотров 78
Data selection in Pandas with "filter"
8:24
Просмотров 2,3 тыс.
Flipping Data with Pandas: Stack & Unstack
8:17
Просмотров 2,4 тыс.
Method chaining in Pandas
18:17
Просмотров 1,7 тыс.
Кто производит iPhone?
0:59
Просмотров 412 тыс.
Развод с OZON - ноутбук за 2875₽
17:48
Will the battery emit smoke if it rotates rapidly?
0:11
Main filter..
0:15
Просмотров 12 млн