how to get the thighest bit position in big integers?

Terry Reedy tjreedy at udel.edu
Thu Oct 9 00:11:51 EDT 2008


greg wrote:
> Terry Reedy wrote:
> 
>> str.find is an historical anomaly that should not be copied.  It 
>> was(is?) a wrapper for C's string find function.  C routinely uses -1 
>> to mean None for functions statically typed to return ints.  The 
>> Python version logically should return None and usually does for other 
>> functions.

I consider str.find to be an anomaly in two senses.

First, it is the only function/method I can think of that directly 
duplicates another function/method by replace raising an exception with 
returning an error indicator.  Some developers wanted to drop it from 
3.0, and I too wish it had been.  We get along fine without list.find 
and a whole slew of other such duplicates that one could think of.

Second, it is the only function/method I can think of that follows the 
Unix/C convention of using '-1' as an error/exception/no-can-do 
indicator.  When it is so used, it is not really an int, but an error 
indication represented as an int because there is no other choice.  In 
Python, there is another choice -- None -- which is used routinely, 
including its use as the default return when there is nothing else to 
return.

> Although Guido has defended it on the grounds that it can
> be inconvenient having a function that returns different
> types under different circumstances. Also it discourages
> making the mistake of treating the return value as a
> boolean.

I consider this backwards.  Since -1 is a legal index in Python (unlike 
some other languages) as well as a legal int, returning -1 allows if not 
encourages the mistake of treating the fake return value as if it were a 
real value.  Returning None would completely prevent any use of the 
error indicator other than to test it, which is the only thing one 
should be able to do with it.  It should not be usable as an index or 
number in further calculations. I consider it a principle that error 
indicators that are returned rather than raised should be an 
illegal-to-use value if not a different type.

As for convenience, "if s.find(t) is None:" is as easily to write (and 
clearer to read) as "if s.find(t) == -1:".  As far as I can see, 
different return types are only an issue, if at all, if values of both 
types are to be used in further calculations.

Terry Jan Reedy





More information about the Python-list mailing list