[Tutor] eval func with floating...

Steven D'Aprano steve at pearwood.info
Wed Aug 24 02:49:50 CEST 2011


Christopher King wrote:
>>     if c:
>>         print *eval("float(%s)"%a)*
>>     else:
>>         print "error! please use -defined operators-!"
>>
> I would use a assert statement for more readability, like so.
> *try: assert c*
> *except AssertionError: print "error! please use -defined operators-!"*
> else: *print *eval("float(%s)"%a)


Only if you want the user to be able to turn off error checking and 
break your program.

"python -O" switches off all assertions.

Do not use asserts except for assertions about internal states of your 
program. Never, ever use them for testing user input, or for reporting 
errors to the user. That is not what they are for. If the user ever sees 
an AssertionError, your code is buggy.



-- 
Steven


More information about the Tutor mailing list