Python Context Managers and the "with" Statement (__enter__ & __exit__)

Python Context Managers and the "with" Statement (__enter__ & __exit__)

Real Python

7 лет назад

69,067 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

vinod kc
vinod kc - 18.11.2022 00:00

Really helpful. Thanks for the video. One question. Is there any performance loss in this approach ?

Ответить
Luke av
Luke av - 10.03.2022 15:34

OMG, use an IDE for crying out loud

Ответить
Igor Simões
Igor Simões - 07.01.2022 04:36

tks, it helps a lot

Ответить
Thuy Trang
Thuy Trang - 22.12.2021 09:22

the video image is too poor, you need to fix it more

Ответить
Dan Córdoba
Dan Córdoba - 06.12.2021 16:19

thanks!

Ответить
Mary Guty
Mary Guty - 23.06.2021 06:15

yield versus return. what is the difference?

Ответить
Mary Guty
Mary Guty - 23.06.2021 06:13

how do you get terminal code prompts and suggestions like in the video? could it do that on pycharm terminal and console?

Ответить
Joshua Rudd
Joshua Rudd - 27.05.2021 01:45

2021 still the best explanation on the web.

Ответить
αηtαgonιsτ
αηtαgonιsτ - 17.05.2021 01:21

2 years of programming in Python and I continue to find things that keep surprising me. Thank you Guido!
Nice video, I love Real Python, always the best and most clear explanations.

Ответить
Gruuvin1
Gruuvin1 - 25.04.2021 14:32

I am now going to write context managed classes for connection objects. Love this!
Note: one can write code to open a file, then read/write a file, but may or may not remember to close the file, and even if they do close the file, it's less likely to be done in a 'finally:' clause, so there is no guarantee it will be closed.
The point is, try/finally wouldn't even get used for open/close (because people can write bad code, and it still seems to 'work'). This is why we have a context manager: to make python play nice with the OS.
Also, I think the contextmanager decorator code communicates what it is better than the class code, even to someone who may not well understand decorators and generators.

Ответить
Kevin Riordan
Kevin Riordan - 27.03.2021 04:28

with Much(love):
me.thank(you)

Ответить
- B L A C K .
- B L A C K . - 23.01.2021 03:46

Thanks bro

Ответить
Emanuel Ramneantu
Emanuel Ramneantu - 18.11.2020 15:13

Great explanation, with the user defined class and all. Thank you!

Ответить
Yash Kale
Yash Kale - 24.10.2020 20:20

which code editor is this??

Ответить
kebman
kebman - 11.10.2020 11:31

The only generators I like, are those who generate money........

Ответить
kebman
kebman - 11.10.2020 11:26

with explaind as very return well # (It's very pythonic!)

Ответить
Haoming Wang
Haoming Wang - 17.09.2020 03:16

Which REPL are you using in the video? It looks awesome, is it ptpython?

Ответить
Jason Kenney
Jason Kenney - 29.05.2020 22:13

This saved me a whole line of code, and for that I'm grateful <3

Ответить
Joseff Aragón
Joseff Aragón - 12.05.2020 18:35

Excellent!!! Nice and clear

Ответить
krish J
krish J - 29.04.2020 18:03

Thanks...

Ответить
Daniel Coimbra
Daniel Coimbra - 27.04.2020 03:31

Wow, I'm really thankful for your explanation!

Ответить
Stewart McNab
Stewart McNab - 21.03.2020 11:45

What is the point of "f"?

Ответить
produdeyay
produdeyay - 17.03.2020 08:21

17 March 2020 - One word, Amazing and thanks!

Ответить
John Wolf
John Wolf - 08.02.2020 18:45

Maybe you should close the video by saying: "Use the 'with open' syntax to optimize your file opening code realizing what's going on structurally within python"

Ответить
hard & code
hard & code - 16.01.2020 18:56

Nicely explained sir. I'm ur new subscriber. Thanks a lot for this vedio.

Ответить
Wellington P Ferreira
Wellington P Ferreira - 12.01.2020 06:24

Very interesting! I got a little confused on the last example though xD
Is not the decorator already closing the file, making the f.close() inside managed_file function a little redundant?

Ответить
Kelly Marchisio
Kelly Marchisio - 22.10.2019 22:32

really excellent. one of the clearest technical explanations i've seen

Ответить
BasikAttack
BasikAttack - 18.10.2019 00:28

What do you mean by "leak resources" by not using with?

Ответить
Anutech Technical Resources
Anutech Technical Resources - 18.09.2019 22:41

2019 still best explanation on the web.

Ответить
Landro
Landro - 07.07.2019 23:18

In my case, the program sometimes RANDOMLY outputs the keys with the ASCII table form.

Ex: "\x08" pressed



sometimes it works ok. anyone knows why

Ответить
Manish Jain
Manish Jain - 07.07.2019 04:01

This video stucks after few seconds. Doesn't move after about 40sec. Kindly upload again.

Ответить
Narasimha Murthy
Narasimha Murthy - 06.03.2019 13:02

Randomly I ended up here found this too good. Now I am gonna watch all of these.

Ответить
Slow Snail
Slow Snail - 28.01.2019 13:40

Could you please tell me how the ,, exit file " would work after that I have written "printed" an output to a text file ?
Thanks in forward :)

Ответить
David Clawson
David Clawson - 17.01.2019 17:36

I was just wondering about this the other day. Super clear explanation. Thanks!

Ответить
Vasiliy
Vasiliy - 30.12.2018 19:36

Nice video, thanks!

Ответить
Daniel Saldivar
Daniel Saldivar - 03.08.2018 17:00

This is the best god damn video on the "with" statement I have ever seen.

Ответить
Hùng Ng V
Hùng Ng V - 17.05.2018 19:16

Thank you so much!

Ответить
Nishant Varma
Nishant Varma - 05.05.2018 10:42

Hey, nice one.

Ответить
LicenseToThrill
LicenseToThrill - 26.04.2018 16:23

Noob question -- can I use my own function in the statement and it operates normally?

Like,

def my_function( ):
complex junk here

with open('file') as fileObj:
my_function( )

Ответить
Elena Oat
Elena Oat - 28.10.2017 15:57

I believe you should put the line where you open the file outside of the try statement. Otherwise, if the file doesn't exist, an error is raised, and in addition in finally you call the close on the file handler var which wasn't created. As a result, 2 errors raised.

Ответить
Matt Cale
Matt Cale - 15.08.2017 19:20

Beautifully done. Do the mugs on your site support you?

Ответить
Mushfiqur Rahman
Mushfiqur Rahman - 13.08.2017 12:04

Hi, thank you very much. Your videos are really helpful. Well explained! Kudos to you.

Ответить
Republic
Republic - 02.07.2017 07:46

This is way more helpful than the stack overflow page. Thanks

Ответить
Marius Wirtz
Marius Wirtz - 25.05.2017 21:25

Hi Dan,
do you plan to release a printed version of your book in the future?

Ответить
Aaron Howard
Aaron Howard - 11.05.2017 00:34

I enjoy your videos and I will be looking for your book(s).

Ответить