Something or Nothing^H^H^H^H^H^H^HEmpty

Petr Prikryl Answer.via.news.please.prikryl at skil.nospam.cz
Wed Apr 10 10:39:37 EDT 2002


Anders J. Munch wrote:
> Boris Borcic wrote...
> > 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.
> > >
> > > [] and {} are objects, they are objects with
> > > identity, and they are mutable objects.  They are
> > > empty, but they are by no means "nothing".
>
> > I could discuss the "Something" side of the
> > matter, but I feel the point you raise here very
> > much boils down to "the type of zero is a
> > manifest information that disallows viewing the
> > typed zero as a nothing".

> No, I was specifically discussing how objects
> created by the expressions []and {} are poorly
> described by "nothingness", and how for this
> reason equating pythonic falsity with nothingness
> can lead you astray, as shown in the example.

I fully agree with Anders.  Indroducing fuzzy
concepts like something vs. nothing may be nice
in philosophical discussions.  Programming
requires somehow more exact approach.  Thinking
in true/false terms is much clearer and
definitely not more difficult.

Here is the snippet from my "True/False story...
(PEP 285)".  The HTML version is temporatily available at
http://www.skil.cz/test/TrueFalseStory.html.


The implicit conversion
~~~~~~~~~~~~~~~~~~~~~~~

The key point which clearly fits with conditional
expressions of the boolean type is called **the
implicit conversion concept**.

In my opinion, explaining the concept of the
*implicit conversion* together with the *context*
where the construct is used allows one to explain
the things better than *something* vs. *nothing*
concept.  I am going to show that.

The Python 2.2.1c2 documentation (i.e. before
bool era) says in *2.2.1 Truth Value Testing*

  Any object can be tested for truth value, for
  use in an if or while condition or as operand
  of the boolean operations below. The following
  values are considered false:

  - None
  - zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  - any empty sequence, for example, '', (), [].
  - any empty mapping, for example, {}.
  - instances of user-defined classes, if the
    class defines a __nonzero__() or __len__()
    method, when that method returns zero.

  All other values are considered true -- so
  objects of many types are always true.

  Operations and built-in functions that have a
  boolean result always return 0 for false and 1
  for true, unless otherwise stated. (Important
  exception: the boolean operations "or" and
  "and" always return one of their operands.)

In other words, it says that in the *boolean
context* the named cases are *converted* to
*false* or *true* values respectively.  So, there
is nothing like...

::

  > {} is a dictionary-shaped nothing. [] is a
  > list-shaped nothing. 0 is an integer-shaped
  > nothing. 0.0 is a float shaped nothing.

Again, it only obscures the things.  The students
should have asked "What do you mean by the
*shaped nothing*?".  (At least for me, it is far
more obscure than the simple notion of yes/no or
true/false.)

Compare it with what the documentation already
says (in different wording):

  When an object appears in a boolean context,
  the implicit conversion is done.  The following
  objects are implicitly converted to False:

  - None
  - zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  - any empty sequence, for example, '', (), [].
  - any empty mapping, for example, {}.
  - instances of user-defined classes, if the
    class defines a __nonzero__() or __len__()
    method, when that method returns zero.

  All other objects are implicitly converted to True -- so
  objects of many types are always converted to True.

--
Petr Prikryl (prikrylp at skil dot cz)






More information about the Python-list mailing list