unittest: assertRaises() with an instance instead of a type

Terry Reedy tjreedy at udel.edu
Thu Mar 29 11:04:47 EDT 2012


On 3/29/2012 3:28 AM, Ulrich Eckhardt wrote:

>> Equality comparison is by id. So this code will not do what you want.
>
>  >>> Exception('foo') == Exception('foo')
> False
>
> Yikes! That was unexpected and completely changes my idea. Any clue
> whether this is intentional? Is identity the fallback when no equality
> is defined for two objects?

Yes. The Library Reference 4.3. Comparisons (for built-in classes) puts 
is this way.
"Objects of different types, except different numeric types, never 
compare equal. Furthermore, some types (for example, function objects) 
support only a degenerate notion of comparison where any two objects of 
that type are unequal." In other words, 'a==b' is the same as 'a is b'. 
That is also the default for user-defined classes, but I am not sure 
where that is documented, if at all.

-- 
Terry Jan Reedy




More information about the Python-list mailing list