automatic type conversion for comparison operators
danb_83 at yahoo.com
Fri Jul 27 01:28:27 CEST 2007
On Jul 26, 6:22 pm, Russ <uymqlp... at sneakemail.com> wrote:
> I posted a message on this several days ago, but it apparently got
> in googlespace, so I'll try it again.
> I recently discovered a bug in my code that apparently resulted from
> the automatic conversion of a function pointer to an integer.
> Say you have a class member function called getCount(), which
> returns an integer. Now suppose you write something like
> if obj.getCount < 3: ...
> This is an error because the parentheses were left off.
BTW, are you a former Pascal programmer?
> But Python
> somehow compares the function pointer with an integer without
> complaining. Unless there is a darn good reason for allowing
> comparisons of this type (and I can't think of one), I think
> Python should flag this as an Exception.
IIRC, the reason is for backwards compatibility with a long-obsolete
version of Python in which it wasn't possible for __cmp__ to throw an
exception. That's not darn good, so this behavior is already slated
for removal in version 3.0.
More information about the Python-list