[pypy-dev] __builtin__ module
Bengt Richter
bokr at oz.net
Sat Jan 25 01:52:34 CET 2003
At 10:23 2003-01-24 -0600, Nathan Heagy wrote:
>>BTW the syntax "i not in range(256)" is more conceptual than "i<0 or i>=256"
>>and expresses better what we are testing for (as the error message suggests
>>too).
>
>Why not "if 0 < i < 256:" ?
I think it's because your ineqality refers to an interval
in the set of all integers (or reals for that matter), whereas
the concept of chr derives from the ordinal place of a character
in a specific finite _ordered_ set of characters. IOW if the char
set were represented as a sequence named charset, then the index
test really should be "i not in range(len(charset))".
If you think of charset having a 1:1 corresponding indexset,
then c = chr(i) can be expressed as
c = charset[indexset.index(i)]
and conventionally the index set for ord is range(len(charset)),
because that ordered set naturally represents position in the set.
Note that you could have a non-integer i and index set in this
formulation, so Armin's test doesn't just test a numerical value
within an interval, it tests whether the index is an allowable index
object (i.e., member of the indexset). Of course if indexset is
range(x) and i is in indexset, indexset.index(i) is a noop, so after
Armin's test, you can safely write c = charset[i].
For better conceptual purity (and less-magical magic numbers ;-),
I might write your inequality as
if isinstance(i, int) and ord(charset[0]) <= i <= ord(charset[-1])
and let ord(c) be charset.index(c)
Maybe Psyco would ultimately generate the same code ;-)
Regards,
Bengt
More information about the Pypy-dev
mailing list