Thinking about the Python equivalents of C's !! double negation
C's !! double negation idiom is a way of compressing all non-zero
values of an expression down into a 1. There's a number of things that
you can use to get the same effect in Python:
I'm not sure which one I like better. Consider an expression like:
nbytes = nbits // 8 + int(bool(nbits % 8))
My C heritage says that this version is the clearest (and indeed
it's the first one I thought of). I'd have to pause to think about
the cmp() version, partly because I've yet to memorize which way
cmp()'s arguments run so I always have to think about what its result
will be.
I suspect that none of these would be considered good Python style,
and that the proper Pythonic way to do this is to just use an if,
or to write the whole thing as math.ceil(nbits / 8.0) and trust
that floating point precision is not going to bite you.
(For some reason I have a small addiction to little overly clever
Python idioms like this.)