[Tutor] Dots-And-Boxes
Kalle Svensson
kalle@lysator.liu.se
Sun, 2 Jun 2002 18:27:59 +0200
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[Pijus Virketis]
> Reading through Danny's code, I found a frequently used keyword
> "assert", like so:
>
> assert 2 <= self.width and 2 <= self.height,\
> "Game can't be played on this board's dimension."
>
> I've never seen if before, so I checked the Reference, and found a
> description here:
> http://www.python.org/doc/current/ref/assert.html. However, the
> syntax given there is different:
>
> if __debug__:
> if not expression: raise AssertionError
This is the eqivalent replacement syntax for
assert expression
> Also, the keyword itself is not really mentioned. So, my question is
> this: what does "assert" do, and where can I find out more about its
> use the way Danny employs it?
The assert statement evaluates an expression, and if the result is
false (0, None, [], () or "", for example) raises an AssertionError.
This only happens if the built-in variable __debug__ is true.
Thus:
assert a == b
and
if __debug__:
if not a == b:
raise AssertionError
do the same thing.
__debug__ is always true if you haven't started python with the -O
option. For example:
: kalle@chloe [~]$ ; python -O -c "assert 0; print 'Hi'"
Hi
: kalle@chloe [~]$ ; python -c "assert 0; print 'Hi'"
Traceback (most recent call last):
File "<string>", line 1, in ?
AssertionError
Peace,
Kalle
- --
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>
iD8DBQE8+keJdNeA1787sd0RApPXAKDIMTMxwawwq/nmnxS5kQikVcy97ACgqJrh
+e8UgNtFiAQwG9K99dw3nWc=
=kybv
-----END PGP SIGNATURE-----