sys.argv and while loop

Alex Martelli aleax at aleax.it
Thu May 9 02:25:34 EDT 2002


Julia Bell wrote:
        ...
> If I use string.atoi(variable) or string.atof(variable), but variable
> cannot
> be converted to an integer or float, I get an error.  I'm just learning
> about catching exceptions, so I think I could figure out how to catch the
> error and
> do something appropriate if that's the recommended approach.  But, is
> there a better way (perform the test before trying to do the conversion)?

Not really.  Given that the amount of work for parsing a float, or parsing
and integer and ensuring it's within sys.maxint, dominates; why would you
want to do it TWICE, once just to get a yes/no answer to the "is it OK"
question, then AGAIN if OK to get the real value?

This reminds me of the polite way one is supposed to use (in Italian) to
ask a stranger about something.  "May I ask you {what time it is / the
way to the station}?" or "Do you know {what time it is / the way to the
station}?".  Taking this literally one could answer "yes" and leave it at
that.  Yes, I do know the way to the station.  That's what I was asked --
whether I know it.  If the enquirer wants me to tell what that way is, he
or she would presumably then have to ask again with a more direct
wording.  Can you imagine the exchange...?

"Sir, do you know the way to the station?"  "Yes I do"
"Well, may I ask you the way to the station?"  "Yes you may"
"So, would you mind telling me the way to the station?"  "No, I wouldn't".
Some enquirers would be edgy at this point...

Is THIS the kind of dialogue you want your program to have with the
Python library?  Why?  Wouldn't it be better to ask directly about the
way to the station, knowing the answer will be "sorry I don't know it"
(or whatever) if need be?


Alex




More information about the Python-list mailing list