cleaner way to write this?

Paul Rubin http
Wed Oct 25 15:02:45 EDT 2006


John Salerno <johnjsal at NOSPAMgmail.com> writes:
> But if the user doesn't enter any text, I don't want the method to
> return at all (even None). I need an error dialog to pop up and tell
> him to enter a value (or press Cancel).

Oh, I see.  You really want something like repeat...while, which Python
doesn't have.  Anyway I start to prefer something like (untested):

     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()

If the logic were more complex you could also create an internal
exception class to deal with the user hitting cancel.



More information about the Python-list mailing list