Rajarshi wrote: > This is a slightly naive question, but I know that 0 can be used to > represent False. So
>>>> 0 == False > True
> But, I know I can use [] to represent False as in
>>>> if not []: print 'empty' > ... > empty
> But then doing the following gives a surprising (to me!) result
>>>> [] == False > False
> Could anybody point out why this is the case?
"if foo:" does not check if "foo == True" or "foo == False" but rather "bool(foo)". For empty lists, strings, tuples, dicts and some other things, "bool(foo) == False", while for lists, etc., with at least one element, "bool(foo) == True".
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
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.
> This is a slightly naive question, but I know that 0 can be used to > represent False. So
> >>> 0 == False
> True
> But, I know I can use [] to represent False as in
> >>> if not []: print 'empty'
> ... > empty
> But then doing the following gives a surprising (to me!) result
> >>> [] == False
> False
> Could anybody point out why this is the case?
> Thanks, > Rajarshi
This has *got* to rank up there among the VFAQ's of them all, along with the mysterious shared default empty list argument. I think this particular question has been asked in one form or another at least twice a week for the past month!
-- Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis Woman was God's _second_ mistake. -- Friedrich Nietzsche
Rajarshi <rajarshi.g...@gmail.com> wrote: >This is a slightly naive question, but I know that 0 can be used to >represent False. So
>>>> 0 == False >True
>But, I know I can use [] to represent False as in
>>>> if not []: print 'empty' >... >empty
>But then doing the following gives a surprising (to me!) result
>>>> [] == False >False
>Could anybody point out why this is the case?
False is just a constant. 0, (), '', [], and False are all constants that happen to evaluate to a false value in a Boolean context, but they are not all the same.
As a general rule, I've found code like "if x == False" to be a bad idea in ANY language. -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc.
In article <1179983482.452458.188...@q66g2000hsg.googlegroups.com>, Paul McGuire <pt...@austin.rr.com> wrote:
> This has *got* to rank up there among the VFAQ's of them all, along > with the mysterious shared default empty list argument. I think this > particular question has been asked in one form or another at least > twice a week for the past month!
Anyone who finds this surprising, might enjoy reading this article from the time several years ago when the feature was being considered. When you have some time - it's long, but interesting. The present confusion is more directly addressed towards the end. Yes, it's the Laura Creighton article again:
Donn Cave wrote: > Anyone who finds this surprising, might enjoy reading this > article from the time several years ago when the feature > was being considered. When you have some time - it's long, > but interesting. The present confusion is more directly > addressed towards the end. Yes, it's the Laura Creighton > article again: