3 ms·
The need to add cleanup code is a change to the interface. If you don't change the interface, you've left yourself open to bugs around accidentally using an in
by xapata 6y ago
The need to add cleanup code is a change to the interface. If you don't change the interface, you've left yourself open to bugs around accidentally using an instance after it's been cleaned.
If you really want to keep the same interface, add a layer of abstraction. The current interface becomes a wrapper around the underlying context manager.
- MereInterest 6y agoExactly. The language design of Python means that the addition of cleanup code is an API change. The language design of C++ means that the addition of cleanup code is not an API change.
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- xapata 6y agoI actually appreciate that API change, as it makes my use of resources more explicit. However, you might be interested in using ``weakref.finalize`` to keep that C++ style. https://docs.python.org/3.6/library/weakref.html#comparing-finalizers-with-del-methods https://docs.python.org/3.6/library/weakref.html#comparing-f...