[Tutor] Another assert() question

Martin Walsh mwalsh at groktech.org
Sun Jul 13 06:07:36 CEST 2008


Dick Moores wrote:
> At 07:39 PM 7/12/2008, Kent Johnson wrote:
>> On Sat, Jul 12, 2008 at 6:03 PM, Dick Moores <rdm at rcblue.com> wrote:
>> > At 01:34 PM 7/12/2008, Kent Johnson wrote:
>>
>> >> In [2]: assert(False, "Asserted false")
>> >>
>> >> This is "assert condition" where the condition is a tuple with two
>> >> elements, hence true so there is no output.
>> >
>> > In [13]: assert(3 < 2 , "qwerty")
>> >
>> > In [14]:
>> >
>> > I don't understand that logic. Could you unpack it for me?
>>
>> (False, "Asserted false") is a tuple containing two values, False and
>> "Asserted false".
>>
>> "assert x" evaluates x as a boolean; if it evaluates to False, the
>> assertion is raised. A tuple with two elements will always evaluate to
>> True so the assertion is never raised.
> 
> But why will a tuple with two elements will always evaluate to
> True?
> 
> In [2]: (3,5) == True
> Out[2]: False
> In [3]: ("qwerty", "asdfg") == True
> Out[3]: False
> In [4]:

You might find it easier to think about this way:

In [1]: bool((3, 5))
Out[1]: True

In [2]: bool(("qwerty", "asdfg"))
Out[2]: True

More info here:
http://docs.python.org/lib/truth.html
http://docs.python.org/lib/node34.html

HTH,
Marty


More information about the Tutor mailing list