Execute command inside loop only upon status change
<p>I'm looking for a smooth (and if possible pythonic) way of executing something inside a while True loop only once per status change, and then if something changes it should print out the new change once rather than spam the console with whatever the current value is.</p>

<p>My general code:</p>

<pre><code>def function()

  while True:
        check_status() #External function that returns a new status value if it changes
        print check_status()

        if status == 0:
            do_something()
            continue
        if status == 1:
            do_something_else()
            continue
function()
</code></pre>

<p>I would put all the tasks into a dictionary, then you can simply track the previous status and only execute a new task when a new status has been reached, something like this:</p>

<pre><code>from time import sleep

tasks = {1:do_something,
         2:do_something_else}

prev_status = None
while True:
    status = check_status()
    if status != prev_status:
        prev_status = status
        print "status changed to: {}".format(status)
        tasks[status]()
    sleep(.1)
</code></pre>

python python-2.7 loops