[Mailman-Users] Python's "false"

Barry A. Warsaw bwarsaw at cnri.reston.va.us
Thu Dec 2 17:03:11 CET 1999


>>>>> "PH" == Pete Holsberg <pjh at mccc.edu> writes:

    PH> What does Python use for the value of "false"?

- the singleton object None

- numeric zero of all types, i.e. integer 0, float 0.0

- an empty list, tuple, or dictionary, i.e. [], (), {}

- the empty string, i.e. '' or ""

You can also make instances act false by returning a false value from
a __nonzero__() method, e.g.

>>> class Foo:
...  def __init__(self, x): self.x = x
...  def __nonzero__(self): return self.x
... 
>>> f = Foo(0)
>>> if f: print 'not false'
... 
>>> f = Foo(1)
>>> if f: print 'not false'
... 
not false


See also <http://www.python.org/doc/current/ref/lambda.html>

-Barry




More information about the Mailman-Users mailing list