home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Truth Value Testing -- Python library reference</TITLE>
- Next: <A HREF="../b/boolean_operations" TYPE="Next">Boolean Operations</A>
- Prev: <A HREF="../t/types" TYPE="Prev">Types</A>
- Up: <A HREF="../t/types" TYPE="Up">Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>2.1.1. Truth Value Testing</H2>
- Any object can be tested for truth value, for use in an <CODE>if</CODE> or
- <CODE>while</CODE> condition or as operand of the Boolean operations below.
- The following values are considered false:
- <UL>
- <LI>• <CODE>None</CODE>
- <LI>• zero of any numeric type, e.g., <CODE>0</CODE>, <CODE>0L</CODE>, <CODE>0.0</CODE>.
- <P>
- <LI>• any empty sequence, e.g., <CODE>''</CODE>, <CODE>()</CODE>, <CODE>[]</CODE>.
- <P>
- <LI>• any empty mapping, e.g., <CODE>{}</CODE>.
- <P>
- <LI>• instances of user-defined classes, if the class defines a
- <CODE>__nonzero__()</CODE> or <CODE>__len__()</CODE> method, when that
- method returns zero.
- <P>
- </UL>
- 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 <CODE>0</CODE> for false and <CODE>1</CODE> for true, unless otherwise
- stated. (Important exception: the Boolean operations `<SAMP>or</SAMP>' and
- `<SAMP>and</SAMP>' always return one of their operands.)
- <P>
-