Wulf's Webden

The Webden on WordPress

With understanding


One of the attractions of my new job, and one that I’m beginning to realise now I’m getting a few months in, is that I’m getting to hone my programming skills. For example, one thing I discovered yesterday was the beauty of the Python’s with statement.

I have to admit that I’d tended to avoid it because I liked the idea of making sure things were tidy. For example, I’d open a file, read it and then close it. However, I got to do a bit more reading yesterday while working on a project and discovered that there is a more ‘Pythonic’ way.

That way is to use with. It is what is known as a context manager and it takes care of all the tidying up, including if something goes screwy and the code doesn’t finish properly. In other words, a better way of opening, reading and closing a file, among other things. It turns out it isn’t that new either; the article I read yesterday was from 2016 but I found another one in the evening that gives a thorough understanding of the topic from 2006.

Well, I guess I’m catching up!

Leave a Reply

Required fields are marked *.

This site uses Akismet to reduce spam. Learn how your comment data is processed.