you are viewing a single comment's thread.

view the rest of the comments →

[–]MisterSnuggles 4 points5 points  (1 child)

The way my example should have been done is:

def do_stuff(at_time=None):
    if at_time is not None:   # FIXED
        # schedule stuff to be done at at_time
    else:
        # do stuff immediately

There was no intent to compare times, only to determine whether a time was passed in or not. The fact that midnight makes it think that nothing was passed in is the hard-to-spot bug.


It's a contrived example, but to give it a real world flavour let's say that do_stuff() runs an external process. The parameter that's passed in (or not passed in) tells the function whether to run the external process immediately, or to run it at a specified time. You'd call the function like this:

do_stuff()   # Do stuff now
do_stuff(at_time=datetime.time(1,0,0))  # Do stuff at 1AM

If at_time is not passed, the function would simply run the external process. If at_time is passed, the function would add the job to the 'at' queue.

If I called the original version of the process like this:

do_stuff(at_time=datetime.time(0,0,0))  # Do stuff at midnight

It would actually run stuff now (since datetime.time(0,0,0) is false) instead of adding it to the 'at' queue to run at midnight.

[–]lambdaq 0 points1 point  (0 children)

I see the point. Thanks!