@CoreySchafer after several months of watching videos on the internet and going through various online training including ones I purchased, I can safely say this channel and specifically Corey Schafer has the best Python training videos on RUclips and perhaps on the entire internet! Thank you very much for taking the time to make these videos and share them with all of us! I find myself repeatedly revisiting your videos as an excellent training reference, well done! I am of course subscribed with notifications turned on and thumbs up! I wish I could give this channel a million thumbs up! I let all the videos play to the end on both my browser and on Chromecast and I hope that boosts your statistics and views, you certainly deserve credit for excellent training and making the Internet a better place for us all! I will be joining your Patreon and helping to support this channel, something I do not often do with limited funding and only reserve the very best content which this certainly is!
Lol, I just started learning python a year ago and now i come across sth in my Documentation that requires some knowledge on Context managers, fuuny part is the next videon is "EXPERT TUTORIAL CONTEXT MANAGERS by Tech With Tim" The expert intro just eased my frustrations for having difficulties to understand this concept fully.
Never thought there is someone who give us such a proper & useful explanation on these lessons! Watching after 3 years of its release still find it way useful! Simply the best on this platform, your big fan from Afghanistan Corey Sir!
As a long-time Java professional, I'm constantly reminded how concise Python is and your excellent tutorials really show it off to its best advantage. please, keep them coming :-)
Don’t know why but halfway through this vid I had an idea for a simple typing practice program. Then I could practice code and get better at typing it too... lmao your genius is rubbing off on me!
A minor note, but the code at 12:51 doesn't actually work if the mode is 'r' and the file doesn't exist in the directory. This is because f never gets assigned a value since open will yield a FileNotFoundError. Therefore, this code will still result in an error because f isn't an object that can be closed. A very minor point, especially since if a file doesn't exist in write mode, one will be created, but in case anyone was curious I double checked this was the case! Great videos as per the usual, Corey!
# I made this with class! # with context manager class ChangeDir: def __init__(self, destination): self.destination = destination def __enter__(self): self.cwd = os.getcwd() os.chdir(self.destination) def __exit__(self, exc_type, exc_val, traceback): os.chdir(self.cwd) with ChangeDir('sample'): print(os.listdir()) with ChangeDir('sample2'): print(os.listdir())
I see it my responsibility to drop a like and say thank you for how well you explain subjects in your videos, as if you really knew what the audience does not understand and you explain that very well. This is just so much better than the courses i paid for.
Hi Corey Please keep making Python tutorials. As many beginners, intermediate concept are already covered please make on advanced topics. They are very helpful...
Corey, thank you for posting these tuts on Python. I've found them all so interesting. I really enjoyed understanding how context managers work and thanks to your video on them I've already made use of one in a project. Thanks again, buddy. They're much appreciated.
Thank you for making our lives easier Corey. Could you please create a series related to advance oops concepts and how and where we can implement them. Thanks!!
Corey, this video is amazing. You're a very good instructor and your contents are really useful and very easy to understand. Thank you for all videos you've been posting.
This was great ... I think I can use a generator in a project I am currently working on. I’m trying to mimic a library I am familiar with in another language that links events with commands through an XML file. I’ll put the project on GitHub for you and your community to review and get some feedback in the next few days. Thanks!
Hey Corey. Excellent video. Your channel is the best on YT in my humble opinion. Can you create a series of videos concerning data structures and algorithms for technical interviews? I think you’d be awesome at it since your teaching ability is 10 out of 10 😄
Great video! Well planned video with first showing the how to recreate something people have used, you then build on that with a simple but great example with the change_dir context manager. Well done!
Hi Corey Shafer, Nice video with a nice explanation.Thank you from the bottom of my heart for ur videos.Hope to see more videos on web and app development using python.Thank you.Bye:-)
thankyou, and indeed Corey is the best Python teacher on the internet. if I may, can you teach us on flutter with Python, ethical hacking, python on raspi...? thankyou!!
Thanks so much for the video. Loved it. However, I am not clear on the last code presented. why did you needed the @contextmanger at top of the function. Would the function still have worked without the context manager?
While I can see cases where the try-except code would be important, in this case it is useless or broken. If the first change directory fails, you will still be in the correct dorectory, so no need to change back. If the get directory fails, then the change directory in the finally clause will fail causing an extra error masking the actual problem.
hi corey, i love your videos so much, can i just make one request and i hope you take it into consideration it will help me and alot of people too can you make 3 playlists about python data scientists libraries(pandas,matplotlib,numpy) it will be very helpful thank you in advance and keep up the good work
Can you explain why do we need round brackets after the class name in here? In your previous videos they were used only for child classes? Thanks in advance for explaining
Thanks for your videos, everything is perfect about them I am working on a program in which a method (no_sync(), a context manager in DistributeDataParallell) is being used like a method to an instance of another class Not sure if I am making any sense
TECHNICAL Question. I would assume that (8:00) 'f' inside the context manager would be just in scope of that context manager. Surprised that printing 'f.closed' even works and not throwing NameError. #edit: omg i have been thinking this the whole time, that conetxt manager has its own scope...
You should call f.flush() after the write opperation. I am supprised the print at the end even returned True as Python isnt writing to disk as soon as you call the write command. this code can lead to incomplete write to disk
Hey Corey, Thanks a lot for the amazing channel and super tut’s. Simply, the best out there! My question here: why you are not catching a possible exception with the “except” clause before you head to the “finally” clause? It seems I’m missing how an exception is been handled should one be raised here Can you please clarify that point? Thanks again.. Mohammed
Hi, reat video! Just one question, do we still need to wrap the codes by a try-except block? Isn't context manager supposed to handle errors automatically?
Great video! Thanks! One question... In the change_dir() example, if there is an error, will it have the original directory in cwd so it can chdir back to it in the finally section?
With-statement does not create a new scope and thus f is still defined outside of the with-statement. You see, the with-statement is equivalent to try-except-finally and try-except-finally doesn't create a new scope. In this case, we try to assign f = open(filename, ...) and finally close it when calling the __exit__(). `f` is still referencing a file object after the with-statement just as it does when getting through a try-except-finally block. Reference: 1) stackoverflow.com/questions/6432355/variable-defined-with-with-statement-available-outside-of-with-block 2) PEP 343
10+ years using Python professionally and I still learning things from Corey.
Still the best python teacher on the web
Thanks! Glad you find these helpful!
@@coreyms Someone stole your video:
ruclips.net/video/LFrGEuncUvk/видео.html
@@snookiewozo Hope @Corey Schafer sees this. That other dude is trying to take credit of something for which he has not done any hard work.
Not still, it's Forever 🙏
Two languages helped to be learnt very well througout tutorials. 1. English 2. Python.
Thanks a lot.
@CoreySchafer after several months of watching videos on the internet and going through various online training including ones I purchased, I can safely say this channel and specifically Corey Schafer has the best Python training videos on RUclips and perhaps on the entire internet! Thank you very much for taking the time to make these videos and share them with all of us! I find myself repeatedly revisiting your videos as an excellent training reference, well done! I am of course subscribed with notifications turned on and thumbs up! I wish I could give this channel a million thumbs up! I let all the videos play to the end on both my browser and on Chromecast and I hope that boosts your statistics and views, you certainly deserve credit for excellent training and making the Internet a better place for us all! I will be joining your Patreon and helping to support this channel, something I do not often do with limited funding and only reserve the very best content which this certainly is!
Thanks, Ernest! That means a lot to me. I'm glad to hear you've found the videos helpful!
i agree
+
This is such a wholesome comment lol, made me feel good : )
Lol, I just started learning python a year ago and now i come across sth in my Documentation that requires some knowledge on Context managers, fuuny part is the next videon is "EXPERT TUTORIAL CONTEXT MANAGERS by Tech With Tim" The expert intro just eased my frustrations for having difficulties to understand this concept fully.
Never thought there is someone who give us such a proper & useful explanation on these lessons!
Watching after 3 years of its release still find it way useful!
Simply the best on this platform, your big fan from Afghanistan Corey Sir!
As soon as I saw Lorem Ipsum as placeholder I knew this guy was legit
As a long-time Java professional, I'm constantly reminded how concise Python is and your excellent tutorials really show it off to its best advantage. please, keep them coming :-)
Don’t know why but halfway through this vid I had an idea for a simple typing practice program. Then I could practice code and get better at typing it too... lmao your genius is rubbing off on me!
A minor note, but the code at 12:51 doesn't actually work if the mode is 'r' and the file doesn't exist in the directory. This is because f never gets assigned a value since open will yield a FileNotFoundError. Therefore, this code will still result in an error because f isn't an object that can be closed. A very minor point, especially since if a file doesn't exist in write mode, one will be created, but in case anyone was curious I double checked this was the case! Great videos as per the usual, Corey!
maybe something like below :
from contextlib import contextmanager
@contextmanager
def open_file(filename,mode):
file = None
try:
file = open(filename,mode)
yield file
finally:
if file is not None:
file.close()
I know this is pretty old video but anyway wanna say thank you for showing what's going on under the hood, it helps with understanding code so much
15:40
if someone is interested doing the exercise using class, refer below :)
class Directorylister:
def __init__(self, destination):
self.cwd = os.getcwd()
self.destination = destination
def __enter__(self):
os.chdir(self.destination)
def __exit__(self, exc_type, exc_val, traceback):
os.chdir(self.cwd)
with Directorylister(""):
print(os.listdir())
Corey is the 👑 of Python, the best in the business.
I just can't help repeating myself. Great Python tutorial. Thank You.
Thanks!
I remember subscribing to your channel but don't how it got unsubscribed. Subscribed again. Wonderful info after 3 years of my python career. Thanks 👍
When it comes to Python teaching, you're by far the best in the entire earth. Thank you so much!
Pure gold. Best tutorials i have ever seen
# I made this with class!
# with context manager
class ChangeDir:
def __init__(self, destination):
self.destination = destination
def __enter__(self):
self.cwd = os.getcwd()
os.chdir(self.destination)
def __exit__(self, exc_type, exc_val, traceback):
os.chdir(self.cwd)
with ChangeDir('sample'):
print(os.listdir())
with ChangeDir('sample2'):
print(os.listdir())
still improving my python skills thanks to you. definitly best
I see it my responsibility to drop a like and say thank you for how well you explain subjects in your videos, as if you really knew what the audience does not understand and you explain that very well. This is just so much better than the courses i paid for.
Thank you!! It is the best tutorial of context managers
I have a white belt in Python and this really helps. Thanks
Hi Corey Please keep making Python tutorials. As many beginners, intermediate concept are already covered please make on advanced topics. They are very helpful...
Even after 3 years pog tutorials
Corey, thank you for posting these tuts on Python. I've found them all so interesting. I really enjoyed understanding how context managers work and thanks to your video on them I've already made use of one in a project. Thanks again, buddy. They're much appreciated.
Love the examples you gave. Very practical!
Thankyou very much for explaining it so cleanly !
Thank you for making our lives easier Corey. Could you please create a series related to advance oops concepts and how and where we can implement them. Thanks!!
Love your tutorials man. Been a huge help!
Simple, to the point & free.
PPL like you make the earth a better place!
Corey, i enjoyed every second of this video, simple and straightforward.
Hope to see a video about coroutines as well.
Thanks.
New super power unlocked!! thanks!
python is just like corey- clear, consise and simple to understand.
Corey, this video is amazing. You're a very good instructor and your contents are really useful and very easy to understand. Thank you for all videos you've been posting.
Gratz on 1mil subs!
with CoreysBestTutorial('Great Explanation', 'Awesome examples'):
set('Like')
execute('Subscribe')
say('Haven't seen better tutorials, than yours!')
That example really helped me understand it really well. Thanks!
Excellent instruction as with all of your Python tutorials. Thank you!
As usual, excellent video Corey !
Crystal clear, as always. Thank you very much..
Excellent tutorial, simple words covers this difficult topic with ease.
clear and simple explanation of of contextmanager, thanks
Thank you so much for this amazing video like many others. This content will keep helping many new developers for always. God bless you.
Very clear - as usual. Excellent.
Explained beautifully
Again a great well thought out and clear presentation. Thanks!!
Congrats on having 200 000 subscribers now!
This was great ... I think I can use a generator in a project I am currently working on. I’m trying to mimic a library I am familiar with in another language that links events with commands through an XML file. I’ll put the project on GitHub for you and your community to review and get some feedback in the next few days. Thanks!
My brain just bursts after watching this video, but super great and useful contents, I just need some pratice...
Hey Corey. Excellent video. Your channel is the best on YT in my humble opinion. Can you create a series of videos concerning data structures and algorithms for technical interviews? I think you’d be awesome at it since your teaching ability is 10 out of 10 😄
Great video! Well planned video with first showing the how to recreate something people have used, you then build on that with a simple but great example with the change_dir context manager. Well done!
Hey Corey Nice video! can't wait for you tutorials on Django and Flask
me2
yeah we sure could use a flask/peewee tut!!!
Really nice video. Explains a lot. Thanks.
Hi Corey, Very good video to unterstand Python contextmanager.
Hi Corey Shafer, Nice video with a nice explanation.Thank you from the bottom of my heart for ur videos.Hope to see more videos on web and app development using python.Thank you.Bye:-)
Beautiful explanation with great examples :) Thank you !!
You are simply the best!
Excellent video! I really need to use context managers more!
You're best Corey!
This video was amazing, thank you so much
thankyou, and indeed Corey is the best Python teacher on the internet. if I may, can you teach us on flutter with Python, ethical hacking, python on raspi...? thankyou!!
Thank you for the great explanation!
So nice explanation i had never seen before. Please make a series of C programming for us.
As usual remarkable video...chorey request you to create bit advance object oriented concept videos
Thank you for this! Best tutorials!
great explanation! thank you
first time i saw your channel i subscribed to it 😁😏
Favorite moment 18:50 - "You can do whatever you wanna .. dir"
As always, I learned something new from your video, thanks
Thanks so much for the video. Loved it. However, I am not clear on the last code presented. why did you needed the @contextmanger at top of the function. Would the function still have worked without the context manager?
This was oo amanzing, and fun to do
Super good explanation thx
this is crazy man so good
Excellent. Thx!!!!
Gold!!!!!! Thank you.
Great video man. Thank you.
Great video thank you.
Thanks for sharing.
While I can see cases where the try-except code would be important, in this case it is useless or broken. If the first change directory fails, you will still be in the correct dorectory, so no need to change back. If the get directory fails, then the change directory in the finally clause will fail causing an extra error masking the actual problem.
Hi I have confusion that if we are using context manager then why we are using try and finally. I think it's automatically handles everything
Well done.
Interesting, thank you
Thanks again.
hi corey, i love your videos so much, can i just make one request and i hope you take it into consideration
it will help me and alot of people too
can you make 3 playlists about python data scientists libraries(pandas,matplotlib,numpy)
it will be very helpful
thank you in advance and keep up the good work
Can you explain why do we need round brackets after the class name in here? In your previous videos they were used only for child classes? Thanks in advance for explaining
Thanks for your videos, everything is perfect about them
I am working on a program in which a method (no_sync(), a context manager in DistributeDataParallell) is being used like a method to an instance of another class
Not sure if I am making any sense
TECHNICAL Question.
I would assume that (8:00) 'f' inside the context manager would be just in scope of that context manager. Surprised that printing 'f.closed' even works and not throwing NameError.
#edit: omg i have been thinking this the whole time, that conetxt manager has its own scope...
Thank you!
you're the man
You should call f.flush() after the write opperation. I am supprised the print at the end even returned True as Python isnt writing to disk as soon as you call the write command. this code can lead to incomplete write to disk
Great video, thanks.
How does the mode attribute gets set to the actual mode attribute of file? Where does that mapping happen?
Hey Corey,
Thanks a lot for the amazing channel and super tut’s. Simply, the best out there!
My question here: why you are not catching a possible exception with the “except” clause before you head to the “finally” clause?
It seems I’m missing how an exception is been handled should one be raised here
Can you please clarify that point?
Thanks again..
Mohammed
great video and great info
please do a video on pickle
ruclips.net/video/S5fYbjBb0zs/видео.html
You are Awesome
Hi, reat video! Just one question, do we still need to wrap the codes by a try-except block? Isn't context manager supposed to handle errors automatically?
Phenomenal
Great video! Thanks! One question... In the change_dir() example, if there is an error, will it have the original directory in cwd so it can chdir back to it in the finally section?
Yes, it should.
6:07 What is the scope of variable `f`? I would expect it to be local in `with` construct, but `f.close()` is placed outside.
With-statement does not create a new scope and thus f is still defined outside of the with-statement.
You see, the with-statement is equivalent to try-except-finally and try-except-finally doesn't create a new scope.
In this case, we try to assign f = open(filename, ...) and finally close it when calling the __exit__(). `f` is still referencing a file object after the with-statement just as it does when getting through a try-except-finally block.
Reference:
1) stackoverflow.com/questions/6432355/variable-defined-with-with-statement-available-outside-of-with-block
2) PEP 343
thank you very much , you are awesome