default object comparison considered harmful?
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Fri May 16 04:12:45 EDT 2008
A.T.Hofkamp a écrit :
> 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?)
Other than implementing a custom __cmp__ function ? Not AFAIK. FWIW, you
don't necessarily need to fiddle with inheritance - you can also
monkeypatch your classes (nb : for new-style objects, you cannot
monkeypatch __magic__ methods on a per-object basis).
Also, IIRC, this behaviour has changed in Python 3.
More information about the Python-list
mailing list