logoalt Hacker News

dist-epochtoday at 12:24 PM2 repliesview on HN

Python has that too, it's called a context manager, basically the same thing as C++ RAII.

You can argue that RAII is more elegant, because it doesn't add one mandatory indentation level.


Replies

mort96today at 3:52 PM

How do you return a file in the happy path when using a context manager?

If you can't, it's not remotely "basically the same as C++ RAII".

logicchainstoday at 1:11 PM

It's not the same thing at all because you have to remember to use the context manager, while in C++ the user doesn't need to write any extra code to use the destructor, it just happens automatically.