type conversions for comparison operators

Russ uymqlp502 at sneakemail.com
Wed Jul 18 23:54:17 CEST 2007

I recently discovered a bug in one of my programs that surprised me
because I thought Python's dynamic type checking would have
caught it.

Suppose I have a function that returns an integer, such as

def numItems: return len(self.items)

Now I want to do a test like this:

if object.numItems() > 2: <do something>

But suppose I mistakenly leave off the parentheses:

if object.numItems > 2: <do something>

I would have thought that Python would choke on this, but it
doesn't. Apparently, Python converts the operands to a common
type, but that seems risky to me. Is there a good reason for allowing
a function to be compared to an integer? Thanks.

More information about the Python-list mailing list