cleaner way to write this?

B

Bruno Desthuilliers

John Salerno a écrit :
But if the user doesn't enter any text, I don't want the method to
return at all (even None).

John, please re-read the FineManual(tm). None is the default return
value of a function - even if there's no return statement.
.... pass
....
 
S

Steve Holden

Paul said:
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.

Of course, in 2.5 you can write

db_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
dlg.Destroy()
return db_name

but frankly I think this is horrible. I suspect we'd better get used to
it, though ...

regards
Steve
 
S

Steve Holden

John said:
Paul Rubin wrote:




Interesting. Some variation of this might suit me, but I think one other
problem I'm having is that the ShowModal() method returns whenever a
button is pressed, so even if OK is pressed with an empty string in the
box, the dialog disappears and returns an "OK" value. I'll have to check
in the wxpython group on how to handle this one, I think.

I suspect you need to use a validator so the user can't click OK until
they've put a value int eh text entry item.

regards
Steve
 
B

Ben Finney

John Salerno said:
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name
else:
dlg.Destroy()
return

It's for reasons like this that I prefer to have only one 'return'
from my functions.

db_name = None
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name
 
B

Ben Finney

Farshid Lashkari said:
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.
 
R

Roy Smith

Ben Finney said:
It's for reasons like this that I prefer to have only one 'return'
from my functions.

db_name = None
if dlg.ShowModal() == wx.ID_OK:
db_name = dlg.GetValue()
dlg.Destroy()
return db_name

Isn't this the kind of thing that the new-fangled "with" statement is
supposed to solve?

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

or something like that. The problem here is that it looks like dlg is
expecting to have Destroy() called instead of just being destructed.
Which, of course, is the problem that RAII is supposed to solve :)
 
P

Paul Rubin

Roy Smith said:
Isn't this the kind of thing that the new-fangled "with" statement is
supposed to solve?

1) that would require rewriting the wx.TextEntryDialog to have an
exit method.

2) It's not necessarily always the case that you want to destroy the
dialog when the function returns. You might want to keep the dialog
around for some reason.
 
J

John Salerno

Steve said:
I suspect you need to use a validator so the user can't click OK until
they've put a value int eh text entry item.

wow, didn't think of that. that might end up being simpler than anything
else i've considered! :)
 
J

John Salerno

Paul said:
Correct, but if the user doesn't enter text, the function is supposed
to loop and prompt for text again, instead of returning.

Yes, that's what I meant. I didn't want it to return *yet* until a value
was entered.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top