That's another good way to do it for sure, and IMO badly underused within the Python ecosystem specifically.
That's called RAII, and I'm a big fan (not of the name, though. It's a terrible name).
https://en.m.wikipedia.org/wiki/Resource_acquisition_is_init...
I've only recently started to think about using factories like that, so I'm very sure there are patterns of dynamic state I wouldn't have a recipe for, but it's a prety appealing idea to me.