default object comparison considered harmful?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri May 16 08:23:34 EDT 2008


Kay Schluehr a écrit :
> On 16 Mai, 10:03, "A.T.Hofkamp" <h... at se-162.se.wtb.tue.nl> wrote:
>> Hello all,
>>
>> Yesterday we found the cause of a bug that has caused problems for a long time.
>> It appeared to be the following:
>>
>> class A(object):
>>     pass
>>
>> print min(1.0, A())
>>
>> which is accepted by Python even though the A() object is not numerical in
>> nature.
>>
>> The cause of this behavior seems to be the compare operation of the object
>> class.
>>
>> Is there a way to disable this behavior in Python (other than deriving a new
>> 'object-like' class that doesn't do comparisons?)
>>
> Are you sure you don't want to use a statically typed language that
> captures all type errors just by inspecting your source code?

This is not necessarily a static vs dynamic typing problem. The 
following code will raise a TypeError for very good reasons:

    print 1.0 + A()



More information about the Python-list mailing list