Message from discussion
0 == False but [] != False?
Path: g2news1.google.com!postnews.google.com!x18g2000prd.googlegroups.com!not-for-mail
From: Raymond Hettinger <pyt...@rcn.com>
Newsgroups: comp.lang.python
Subject: Re: 0 == False but [] != False?
Date: 23 May 2007 22:09:48 -0700
Organization: http://groups.google.com
Lines: 24
Message-ID: <1179983387.883994.67580@x18g2000prd.googlegroups.com>
References: <1179982400.412340.178710@g4g2000hsf.googlegroups.com>
NNTP-Posting-Host: 75.41.40.92
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1179983398 18354 127.0.0.1 (24 May 2007 05:09:58 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 24 May 2007 05:09:58 +0000 (UTC)
In-Reply-To: <1179982400.412340.178710@g4g2000hsf.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: x18g2000prd.googlegroups.com; posting-host=75.41.40.92;
posting-account=xk13MAwAAACifx9-ULoPtlM0x5V_XngZ
> >>> [] == False
> False
>
> Could anybody point out why this is the case?
Writing, "if x" is short for writing "if bool(x)".
Evaluating bool(x) checks for a x.__nonzero__()
and if that method isn't defined, it checks for
x.__len__() to see if x is a non-empty container.
In your case, writing "if []" translates to
"if len([]) != 0", which evaluates to False.
True and False are of type bool which is a subclass
of int. So, False really is equal to zero and
True really is equal to one.
In contrast, the empty list is not of type int.
So [] != False eventhough bool([]) == False.
Raymond