cleaner way to write this?

Farshid Lashkari no at spam.com
Wed Oct 25 14:22:06 EDT 2006


John Salerno wrote:
> Hi guys. I'm looking for a nicer, more compact way of writing this code. 
> It doesn't have to be anything fancy, just something without the 
> duplication and preferably only one return statement.
> 
>     def create_db_name(self):
>         dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:',
>                                  'Create New Database')
>         if dlg.ShowModal() == wx.ID_OK:
>             db_name = dlg.GetValue()
>             dlg.Destroy()
>             return db_name
>         else:
>             dlg.Destroy()
>             return
> 
> One problem is that if "Cancel" is pressed, I can't return anything. 
> Another problem is that the dialog must be destroyed, so that has to 
> come before any return statements.
> 
> Thanks.

This should work:

def create_db_name(self):
         dlg = wx.TextEntryDialog(self.frame, 'Enter a database name:',
                                  'Create New Database')
         db_name = None
         if dlg.ShowModal() == wx.ID_OK:
             db_name = dlg.GetValue()
         dlg.Destroy()
         return db_name


-Farshid



More information about the Python-list mailing list