Zero [was Re: What is considered an "advanced" topic in Python?]
Marko Rauhamaa
marko at pacujo.net
Mon Jun 1 09:18:17 EDT 2015
Steven D'Aprano <steve at pearwood.info>:
> What does zero mean, and how do engineers misunderstand it?
Off the top of my head:
* Code that does:
if elements:
for element in elements:
...
instead of:
for elements in elements:
...
* C++ insists that all objects have nonzero sizes. This program:
====================================================================
#include <stdio.h>
struct S {};
int main()
{
printf("%zd\n", sizeof(struct S));
return 0;
}
====================================================================
prints out 1 when compiled with c++ but 0 when compiled with cc.
* In C, it used to be illegal to define a struct without a dummy field
or a zero-length array. At one point, that created irritating trouble
for compilers that generated C.
* malloc(0) is allowed to return NULL as a successful return value
inviting application programming errors. Calling free(NULL) didn't
use to be guaranteed to work.
* Numerous shell commands assign special meaning to zero arguments.
For example:
$ ls a b c
a b c
$ ls a b
a b
$ ls a
a
$ ls
a b c d e f g
$ ln -s a b c
# creates two links under c, which must be a directory
$ ln -s c
# creates one link under .
* Wild-card expansion always produces at least one result:
for src in *.py; do
# executed even in the absence of a .py file
done
* Python insists that a block contain at least one statement even
though technically, it is not necessary for syntactic reasons. Not a
biggie but can cause a minor annoyance when commenting out a line.
* Numerous timer implementations take 0 to mean infinity. For example,
in Java:
public final void wait(long timeout)
throws InterruptedException
[...] If timeout is zero, however, then real time is not taken
into consideration and the thread simply waits until notified.
<URL: https://docs.oracle.com/javase/7/docs/api/java/lan
g/Object.html#wait%28long%29>
A nasty surprise for those who'd naively expect zero to mean zero,
especially if zero is the result of downward rounding.
Marko
More information about the Python-list
mailing list