[Edu-sig] re: More spillover re the division PEP

Kirby Urner pdx4d@teleport.com
Thu, 26 Jul 2001 09:22:32 -0700


I agree with Art that this really shouldn't have much to
do with "newbies" (how to define? -- new to Python in
particular, or to programming in general; I suppose the
latter).

Forcing type coercian looks a bit easier when you write
3./4 vs p/q, as the former show their types by inspection.
It's p/q that's more problematic because we don't know
what it will do until we either (a) type check or (b)
coerce, on the assumption that p and q are at least
int, float or long.

Suppose p = long('2847509247509234750924752309') and q = 5.
Then p/q returns 569501849501846950184950461L whereas
float(p)/q returns 5.6950184950184698e+026.  We know that
the former is how many times 5 divides evenly into p; there
might still be a remainder (and there is:  4).

Using // in the code tells other programmers that division
of the former kind is expected (tossing out the remainder,
keeping results integer).  / says the result will be float.

You don't need to look elsewhere in the code to try to figure
out if p and q are both ints.  Given this is code you didn't
write, you maybe don't get to force a type coercian either
(who said you could change what's written? -- maybe this is
a commercial app and you job is to understand but NOT tweak).

So you're left trying to discover the secret identities of
p and q, if you want to know how this operator will behave.
In a weakly typed language, that's not easy.  Maybe the coder
has some p *= 1. nearby, but who's to enforce such an idiom?

Now of course we have overdetermination in + as well, i.e.
a + b might concatenate, vs. add numbers.  But it's somewhat
easier to track when a variable is being treated as an
str vs. some kind of number, than it is to be sleuthing the
diff between float, int and long usages.

So I can see the argument for making the return value
explicitly typed thanks to what operator is used (/ or //).
I don't need to think about newbies.  I think about pro
programmers studying code they didn't write and trying to
figure it out.

Kirby





 >>> float()
2.847509247509235e+027
 >>> float(long('2847509247509234750924752309'))/5

 >>> long('2847509247509234750924752309')/5

 >>> divmod(long('2847509247509234750924752309'),5)
(569501849501846950184950461L, 4L)