At 01:34 PM 7/12/2008, Kent Johnson wrote:
On Sat, Jul 12, 2008 at 4:01 PM, Dick Moores <[EMAIL PROTECTED]> wrote:
> _Python in a NutShell_, p. 138 has a bit on the assert statement which I
> don't completely understand.
>
> It says the syntax is
>
> assert condition[, expression]
>
> I was hoping to put some sort of explanation of failure in an assert
> statement. But how to do it?
>
> In my code I have
>
> assert(len(list(set(colors_used_this_cycle))) ==
> len(colors_used_this_cycle), "A color has been used twice!")

Leave out the outermost parentheses, assert is a statement, not a function call.

Ah. OK.

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?


In [3]: assert False, "Asserted false"
---------------------------------------------------------------------------
<type 'exceptions.AssertionError'>        Traceback (most recent call last)

/Users/kent/<ipython console> in <module>()

<type 'exceptions.AssertionError'>: Asserted false

This is the form with an optional expression and works the way you want.

In [14]: assert 3 < 2, "Bad math"
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)

E:\Python25\Scripts\<ipython console> in <module>()

AssertionError: Bad math

In [15]:

Yes.

Thanks, Kent.

Dick
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to