Assertions

alister alister.ware at ntlworld.com
Fri Sep 22 08:03:45 EDT 2017


On Fri, 22 Sep 2017 21:15:54 +1000, Steve D'Aprano wrote:

> On Fri, 22 Sep 2017 08:50 pm, alister wrote:
> 
>>> The bottom line is, if I saw
>>> 
>>> if not (thing > 0): raise AssertionError(...)
>>> 
>>> in a code review, I'd probably insist that either it be changed to use
>>> `assert`,
>>> or the exception be changed to ValueError, whichever better expresses
>>> the intention of the code.
>> 
>> In a code review I would want the condition changed to be less noisy/
>> confusing to the reader.
>> 
>> if thing <=0: whatever
> 
> Fair point, assuming they are the same.
> 
> Actually, even for floats they're not the same.
> 
> py> not (NAN > 0)
> True py> NAN <= 0 False

I bet those are conditions the original author did not expect 

actually I am not sure how you got them on my system

python 2.7

>>> not (NAN >0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NAN' is not defined

>>> NAN <=0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NAN' is not defined


Python 3

Python 3.6.2 (default, Aug 11 2017, 11:59:59) 
[GCC 7.1.1 20170622 (Red Hat 7.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> not (NAN > 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NAN' is not defined
>>> NAN <= 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NAN' is not defined


I would also say that if your examples did indeed return different 
results then pythons logic model has a bug.



-- 
Say "twenty-three-skiddoo" to logout.



More information about the Python-list mailing list