Nov 12

Here was my problem: I needed to reverse a boolean value throughout a loop. True, False, True, False, etc. In most languages, let's say PHP, that looks like this:

$var = !$var;

Python doesn't like the ! when used in this context. However, I got to thinking, and this is what I came up with.

var = var == False

or

var = var != True

Hope this is useful to someone.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit

2 Responses

  1. Stephen Paulger Says:

    >>> var = True
    >>> var = not var
    >>> var
    False

  2. admin Says:

    Oh, cool, I hadn't thought of that. Neat.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.