cleaner way to write this?

John Salerno johnjsal at NOSPAMgmail.com
Wed Oct 25 16:03:10 EDT 2006


Paul Rubin wrote:

>      def create_db_name(self):
>          try:
>             while True:
>                dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:',
>                                        'Create New Database')
>                if dlg.ShowModal() != wx.ID_OK:
>                    return None
>                db_name = dlg.GetValue()
>                if db_name:
>                    return db_name
>                pop_up_error_dialog("please enter a value or press cancel")
>          finally:
>             dlg.Destroy()

Interesting idea to use try/finally to ensure that dlg.Destroy() runs 
even with a return earlier in the method. Would this be considered 
appropriate use though, or would it be misusing try/finally? I thought 
the point of a try block was for when you anticipated exceptions, but 
given that try/except and try/finally used to be separate blocks, 
perhaps this was the original intent of a try/finally block? (i.e. 
nothing to do with exceptions?)



More information about the Python-list mailing list