Something or Nothing^H^H^H^H^H^H^HEmpty
Anders J. Munch
andersjm at dancontrol.dk
Wed Apr 10 04:43:28 EDT 2002
Pythons truth values are not "Something or Nothing". The phrase seems
to have caught on following Laura's post, but I believe it is
misleading.
[] and {} are objects, they are objects with identity, and they are
mutable objects. They are empty, but they are by no means "nothing".
Example:
def show(foo, history=None):
"""prints foo, optionally storing what was printed in a history list."""
print foo
if history:
history.append(foo)
This works as intended:
# Omit default argument, don't remember history
show("hello world")
This works as intended:
history = ["in the beginning was nothing"]
show("hello world", history) # adds "hello world" to history list
But this doesn't:
history = []
show("hello world", history) # strange..history doesn't change here?
It doesn't work, because show() tests for emptyness with "if
show_history:". What show() should have done is test for nothingness
using "if show_history is None:".
--
Anders Munch. Software Engineer, Dancontrol A/S, Haderslev, Denmark
Still confused but at a higher level.
More information about the Python-list
mailing list