cleaner way to write this?

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Oct 25 19:45:06 EDT 2006


Farshid Lashkari <no at spam.com> writes:

> Paul Rubin wrote:
> > I like
> > 
> >     if dlg.ShowModal() == wx.ID_OK:
> >          db_name = dlg.GetValue()
> >     else:
> >          db_name = None
> >     dlg.Destroy()
> >     return db_name
> > 
> > better than
> > 
> >     db_name = None
> >     if dlg.ShowModal() == wx.ID_OK:
> >          db_name = dlg.GetValue()
> >     dlg.Destroy()
> >     return db_name
> > 
> > but I suppose it's a matter of preference.
>
> Yeah, I think the second way is usually used by people who are more
> accustomed to programming in C, since they need to initialize
> variables.  Your way is probably more Pythonic though.

I prefer an unconditional setting of the default value it because it's
more explicit. You can immediately see that db_name will *always* have
a known value, rather than trying to traverse the logic to find the
default. The quicker I can communicate the flow of the function, the
better.

-- 
 \       "From the moment I picked your book up until I laid it down I |
  `\    was convulsed with laughter. Someday I intend reading it."  -- |
_o__)                                                     Groucho Marx |
Ben Finney




More information about the Python-list mailing list