automatic type conversion for comparison operators

R

Russ

I posted a message on this several days ago, but it apparently got
lost
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. 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.
 
D

Dan Bishop

I posted a message on this several days ago, but it apparently got
lost
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.
 
R

Russ

Dan said:
BTW, are you a former Pascal programmer?

No. Why do you ask? [The code snippet I wrote was made up to get a
point across. I
did not actually use that function name in my code.]
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.

That's good to know. Thanks.
 
D

Dan Bishop

Dan said:
BTW, are you a former Pascal programmer?

No. Why do you ask? [The code snippet I wrote was made up to get a
point across. I
did not actually use that function name in my code.]

I just have a hypothesis that former Pascal/Basic programmers are much
more likely than C/C++/Java programmers to forget the () on a function
call. Nothing to do with the (Java-like) function name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top