Closed beniroquai closed 4 years ago
The intention behind using the camera class with context managers is to ensure, that open connections are closed even in case of errors. That is why VimbaPython was designed with them. I would not suggest trying to circumvent them because this might lead to unexpected behavior. Could you perhaps give some more information what you want to achieve? Perhaps I can suggest a different approach that does not involve circumventing the context managers.
With that disclaimer in mind, you can of course "trick" the camera into working without the with
keyword. You already linked to the camera class. A few lines lower (line 361 to 374) you can see what happens when using the with
keyword. When entering the indentation level of the with
block, the __enter__
method is called. When leaving the indentation level the __exit__
method is called. As you can see, on the the first call to __enter__
Camera._open()
is called. This opens the actual connection to the camera and attaches the available camera features as more pythonic "feature_accessors". The __exit__
method does the expected equivalent of closing the camera connection.
This means that if you really want to (or have to) work without the context managers, you can call Camera._open
and Camera._close
manually where appropriate. However as I already said, I would strongly advise against this as you are essentially removing a safety net that was included on purpose.
Thank you very much. This is very helpful and I think this behavior is better in terms of exception-management.
Is it possible to use a camera object as a global class variable object rather than within the
with
-block?I'm referring to this class.
Thanks.