Python Context Managers

Ok, I'm going to go ahead and add the context manager protocol to wx.Dialog. There are also several helper classes in wxWidgets that seem to be a good fit for the pattern so I'll be adding it to them too[1]. Are there any other wx classes or other cases where it would also make sense to add the context manager methods? Suggestions are welcome.

[1] Although in these cases the __enter__ and __exit__ won't actually be doing anything as the C++ class will be doing the real work. Here are the other classes I've modified so far:

  wx.BusyInfo
  wx.BusyCursor
  wx.WindowDisabler
  wx.LogNull
  wx.DCTextColourChanger
  wx.DCPenChanger
  wx.DCBrushChanger
  wx.DCClipper

···

On 2/5/10 11:16 AM, Robin Dunn wrote:

On 2/4/10 11:54 AM, Mark Guagenti wrote:

I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.

That's a very good idea, thanks for bringing it to our attention.

--
Robin Dunn
Software Craftsman