[Tutor] True/False evaluations?
Liam Clarke
cyresse at gmail.com
Wed Dec 1 12:17:28 CET 2004
Hi all,
Got myself in a twist around logic of functions, I'll show you what I mean.
>>> def x():
... return 0
...
>>> def y():
... return []
...
>>> x()
0
>>> y()
[]
>>> x()==False
True
>>> y()==False
False
>>> x()== y()
False
>>> if not x():
... print 'x is false'
... else:
... print 'guess x is true'
...
x is false
>>> if not y():
... print 'y is false'
... else:
... print 'hmmm'
...
y is false
It's y() not being False (or True), but evaluating as true for 'if
not y()' which gets me, as I thought that [], 0, None, False, & {} all
evaluated the same.
General query however -
>>> def k():
... return [], 0
...
>>> print k()
([], 0)
>>> if not k():
... print "It's false"
... else:
... print 'This tuple is going to cause me grief'
...
This tuple is going to cause me grief
So, as k() returns a tuple, it's not false, even though the tuple
contains two zero values. Is there a way I could do something like?
for item in k():
if not item:
#do something
on one line? i.e. check for true/false of any value returned by k(),
whether it be a 2 or 20 digits long tuple
Um,
--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
More information about the Tutor
mailing list