Why bool( object )?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Tue Apr 28 22:54:02 EDT 2009
On Tue, 28 Apr 2009 11:59:18 -0700, Aaron Brady wrote:
>> To steal an idiom from Laura: Python has a float-shaped Nothing 0.0, a
>> list-shaped Nothing [], a dict-shaped Nothing {}, an int-shaped Nothing
>> 0, a singleton Nothing None, and so forth.
>
> The sound of that metaphor is rather pleasing ('sweet nothings'), but
> I'm not so sure that metaphors belong in computer science and
> programming.
Programming models the world, it isn't the world. Every model is a
metaphor. You can't get away from metaphors in programming, you can only
hide them and pretend they aren't there.
> Nothing can't have many shapes.
In computing, you can't have a literal "nothing", because EVERYTHING
needs to be stored as a pattern of bits. In practice, for most high-level
languages, those bits are interpreted as a type as well as a value, so
the same bit pattern can mean different things according to what type it
is expected to be. Alternatively, the same information can be represented
in different bit patterns depending on how you interpret those bit
patterns.
So in the context of computer programming, of course you can have nothing
with many "shapes": you have the number 0 represented as a single byte, a
16-bit integer, a 32-bit integer, a long-int or BigNum, a pointer with
address 0, fixed point decimals of various sizes, Binary Coded Decimal, a
rational, a single float 0.0, a double float 0.0, a string of "tally
marks" without any tally, and so on, all of them in big-endian and little-
endian formats.
> Having no onions is the same as having no carrots.
"If you have one onion and I take it away, how many onions do you have
left?"
If you answered "no carrots", I'd consider that a bizarre and unhelpful
answer, and wonder what you possibly thought carrots had to do with my
question.
"Nothing" is not an absolute. "There is no wind" is not the same thing as
"there is no money left in my bank account".
> If the different shapes of nothing don't
> compare equal to each other, which they don't, then they aren't all the
> same thing in different shapes.
Who said they were the same thing?
> Furthermore, it is awfully presumptuous to give objects a default
> 'nothing-ness' of 'something'. If anything, they should be nothing by
> default.
That's a stupid idea.
There are 4,294,967,296 integers that can be represented in 32 bits. Only
one of them represents zero.
The number of possible strings is unlimited (except by physical
limitations, such as the amount of memory available, or the size of the
universe). Again, only one of them is the empty string.
Likewise for sets, lists, tuples, dicts, binary trees, queues, and just
about every data structure you care to name: at most one such instance is
empty, out of a potential infinite number of instances.
Most data represents *useful information* ("Something"), not the absence
of information ("Nothing"). Why on earth would you want to reverse that
presumption?
> Conversion to other primitives is very picky; why should
> Boolean be so tolerant?
Because it is useful and meaningful to ask whether an arbitrary object
represents something or nothing, but it's not useful or meaningful to ask
what integer is equivalent to an arbitrary object.
--
Steven
More information about the Python-list
mailing list