2

I'm trying to design a class to manage video devices in Linux (/dev/video*).

Because of my C++ background I naturally thought I could open the file in the constructor and close it in the destructor.

Then later I learned python does not guarantee when/if the destructor is called.

Then I think I can make my own "initialize" and "de-initialize" methods to manage the opening/closing of the device file, but then it creates time gaps when the objected is constructed but not initialized and also when the object is de-initialized but not destructed, at which time the object does not have a valid internal state ( the methods are mostly ioctls on the opened video device).

That means I need to validate object state at the beginning of each method , like built-in file objects (f=open(), f.close)? Or just let the I/O error occur when a method is called on an already de-initialized object?

1 Answer 1

1

Go ahead and open the file in the constructor, it won't hurt anything.

Python provides the with statement to allow setup and teardown of an object beyond construction/destruction. Your object must include an __enter__ and __exit__ method; __enter__ is called at the beginning of the with statement, and __exit__ is called at the conclusion of the code block contained within the with. Notably __exit__ is called whether the block runs to completion or is terminated early with an exception.

Obviously with is only useful if you're using the object right then and there, not if you're storing it as a member in yet another object for example. But you can just go one level deeper and use with around that object, and have its __exit__ function call a cleanup function on your own object.

Sign up to request clarification or add additional context in comments.

2 Comments

If I understand correctly, using with statement binds the object to a finite scope and all references to that object are supposed to live within a scope smaller than that scope (or the program is ill-formed). This helps eliminate the possibility of 2nd kind of dangerous time gap (that is, "what if the object is torn down but someone still hold reference to it?") because after the scope ends no one is suppose to hold a reference to it. At the same time opening the file in the constructor eliminates the first kind of time gap. Two methods combined solve the problem?
No, a with statement doesn't prevent other users from keeping references to your object. If some operations on your object are invalid after it's been closed, you probably should raise an exception in those situations (you might want to have a closed attribute on your object to make this easy to test for). Being compatible with with statements just makes it easy for your users to close your file automatically when they're done with it (and not leave it open if something goes wrong).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.