[Tutor] Why begin a function name with an underscore

Steven D'Aprano steve at pearwood.info
Wed Aug 29 18:36:54 CEST 2012


On 28/08/12 21:24, Wayne Werner wrote:
> On Mon, 27 Aug 2012, Richard D. Moores wrote:

>> What the best way to test if something's an integer?
>
> try:
>     whatever_you_want(supposed_integer)
> except ValueError:
>     print("Oops, that wasn't an integer! Please try again")
>
> That's usually the best way...

Actually, that's close to the worst way, since you take a nice, useful
exception which prints a traceback showing exactly what went wrong, and
replace it with a pointless, silly message which can't be caught by the
caller.

Trying again may be impossible, or inappropriate, and certainly isn't
up to the function to make that decision, that's up to the caller to
decide what is the appropriate response to invalid data.



-- 
Steven


More information about the Tutor mailing list