471,350 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

type conversions for comparison operators

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.

Jul 18 '07 #1
0 698

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
3 posts views Thread by Rene | last post: by
51 posts views Thread by jacob navia | last post: by
7 posts views Thread by Madhu Gopinathan | last post: by
12 posts views Thread by Just D | last post: by
4 posts views Thread by swansnow | last post: by
12 posts views Thread by Joe Strout | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.