Something or Nothing^H^H^H^H^H^H^HEmpty
Magnus Lie Hetland
mlh at vier.idi.ntnu.no
Wed Apr 10 22:10:40 EDT 2002
In article <3cb3fbc0$0$78808$edfadb0f at dspool01.news.tele.dk>, Anders
J. Munch wrote:
>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.
I don't.
>[] and {} are objects,
Yes, as are None and 0.
> they are objects with identity, and they are
>mutable objects. They are empty, but they are by no means "nothing".
Would you call them false? Would you call 0 empty? It's an intuitive
way of explaining the concept, not a precise one. Nothing is nothing,
and as long as you try to represent it with a value (which must, by
necessity, be something, you will fail).
>Example:
>
>def show(foo, history=None):
> """prints foo, optionally storing what was printed in a history list."""
> print foo
> if history:
> history.append(foo)
Very odd function. "If there is a history" (i.e. the history list
contains something) "append something else"...
I don't think this is less clear if one considers the empty list as
"nothing". If your history is represented as a history, I'd say you
had no history if the list were empty.
The fact that you're using None as a placeholder means that you
shouldn't use a plain boolean check for the history here -- you should
use the standard idiom:
def show(foo, history=None):
print foo
if history is not None:
history.append(foo)
or perhaps:
def show(foo, history=None):
print foo
try: history.append(foo)
except TypeError: pass
(might be more efficient but will hide the error of passing a
non-appendable object as history.)
>This works as intended:
[snip]
>But this doesn't:
>
> history = []
> show("hello world", history) # strange..history doesn't change here?
IMO that should be obvious from your function definition. I don't see
what it is you feel this example proves. That seeing the empty list as
"a list-shaped nothing" (in Laura's words) would make people think
that only None qualifies as false (as opposed to an empty list)? I
really don't understand your interpretation here.
>It doesn't work, because show() tests for emptyness with "if
>show_history:".
Of course.
> What show() should have done is test for nothingness
>using "if show_history is None:".
No -- it should have tested whether history was the default value you
provided. This is not a test of nothingness -- it's not even a test of
None-ness (using the "ness" ending to indicate structural polymorphic
equivalence, which is normal in Python); it's a test of whether or not
the object is identical to None.
--
Magnus Lie Hetland The Anygui Project
http://hetland.org http://anygui.org
More information about the Python-list
mailing list