4 ms·
Python context managers somewhat require it. You have to create an object on which you can call `__enter__`.
by banthar 1y ago
Python context managers somewhat require it. You have to create an object on which you can call `__enter__`.
- ptsneves 1y agoIt is a tragedy that python almost got some form or RAII but then figured out an object has 2 stages of usage. I also strongly disagree constructors cannot fail. An object that is not usable should fail fast and stop code flow the earliest possible. Fail early is a good thing.
- jakewins 1y agoThere is no contradiction between “constructors cannot fail” and “fail early”, nobody is arguing the constructor should do fallible things and then hide the failure. What you should do is the fallible operation outside the constructor, before you call __init__, then ask for the opened file, socket, lock, what-have-you as an argument to the constructor. Fallible initialisation operations belong in factory functions.
- int_19h 1y agoThe real problem is that constructors and factory functions are distinct in the first place. They aren't, in Rust, and it's much easier to reason about and requires far less verbiage to write.
- throw-the-towel 1y agoWhy though?
- jakewins 1y agoThis is the exact opposite? They explicitly encourage doing resource-opening in the __enter__ method call, and then returning the opened resource encapsulated inside an object. Nothing about the contract encourages doing anything fallible in __init__