Numeric array in unittest problem

Alex Martelli aleax at mail.comcast.net
Mon Nov 21 10:59:59 EST 2005


ajikoe at gmail.com <ajikoe at gmail.com> wrote:

> Sorry Peter,
> 
> Try this....
> 
> import unittest
> import Numeric
> 
> class myTest(unittest.TestCase):
>     def runTest(self):
>         var1 = Numeric.array([1,22])
>         var2 = Numeric.array([1,33])
>         self.assertEqual(var1,var2)
> 
> if __name__ == '__main__':    
>     unittest.main()

Try this interactively and you'll see why:

>>> import Numeric
>>> a=Numeric.array([1,22])
>>> b=Numeric.array([1,33])
>>> c = a==b
>>> c
array([1, 0])
>>> assert(c)

i.e., thanks to element-by-element evaluation, == will generally return
a true value for ANY comparison of Numeric arrays, causing a very
frequent beginner's bug to be sure.  Try Numeric.alltrue(c), or
Numeric.allclose(a,b) ...


Alex



More information about the Python-list mailing list