I'm not sure what Steven was trying to say there, but for me:
jar with no jellybeans == 0
no jar == None
The existence of a jar or no jar is irrelevant to the question of how
many jellybeans there are. They are two different things, and therefore
need two different values. There are many ways to implement this.
# One way
jar_exists = True # or possibly False
jellybeans = 42 # or whatever the number is, possibly 0
# Another way
jar = Jar(number_of_beans=jellybeans) if jar_exists else None
jellybeans = jar.jellybeans if jar is not None else 0
If you have many jars, and you want to count the total number of
jellybeans:
total = sum(jar.jellybeans for jar in jars if jar is not None)
Strictly speaking, the "is not None" is redundant, but it expresses the
intent better than the alternative. Assuming that jar instances follow
the standard Python API for containers, and is treated as falsey when it
has a jellybean count of zero:
total = sum(jar.jellybeans for jar in jars if jar)
# relies on the fact that adding zero to a number makes
# no difference, so you can safely leave zeroes out
Here's a case where you *must* distinguish between empty jars and None:
number_of_jars = sum(1 for jar in jars if jar is not None)
and a case where you *shouldn't*:
number_of_nonempty_jars = sum(1 for jar in jars if jar)
Of course you can write this:
number_of_nonempty_jars = sum(
1 for jar in jars if jar is not None and jar.jellybeans > 1
)
but that overwhelms the code with incidental implementation details about
jellybean counts, which is prone to bugs. (Did you spot it?)
Even jar.isempty() would be better, but better still is to *not* invent
your own container API but to use the standard Python one instead and
define an appropriate __nonzero__ (Python 2) or __bool__ (Python 3)
method.
If I insist on making a single object do duty for both the jar and the
jellybean count, then I need a "null jar object", and I probably end up
with something like this:
Jar(number_of_beans=None) => null jar object with jar.jellybeans = 0
Jar(number_of_beans=0) => normal jar object with jar.jellybeans = 0
Jar(number_of_beans=42) => normal jar object with jar.jellybeans = 42
and then my code becomes even more complicated and less understandable,
but hey, it's *my* code and I can do whatever damn-fool thing I like!