This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author amaury.forgeotdarc
Recipients
Date 2003-11-21.13:08:39
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
When the first operand of "and" results in False, its truth 
value is calculated again.

Example:
class myBool:
  def __init__(self,value):
    self.value = value
  def __nonzero__(self):
    print 'testing myBool(%s)' % self.value
    return bool(self.value)

if myBool(0) and myBool(1):
  pass

will print:
testing myBool(0)
testing myBool(0)

The same thing occurs with the "or" operator, when the 
first argument has a True truth value:

if myBool(2) and myBool(3):
  pass
will print:
testing myBool(2)
testing myBool(2)

This can be a problem when the "__nonzero__" function 
is slow or has side-effects. I agree this is not often the 
case...

But imagine an object which truth value means "there 
are more data to read in a stream". If python evaluates 
__nonzero__ twice, the expression: "stream and 
otherTest()" can become True *without* evaluating the 
otherTest!
History
Date User Action Args
2007-08-23 14:18:22adminlinkissue846564 messages
2007-08-23 14:18:22admincreate