This is a bit more complicated. First, notice that we used animportstatementinsidethedebug()function to import thetime()function from thetimemodule. This allows us to avoid having to add thatimportoutside the function and risk forgetting to remove it.
Look at how we definedtimestamp. It is a function parameter with a list as its default value. InChapter 4,Functions, the Building Blocks of Code, we warned against using mutable defaults for parameters because the default value is initialized when Python parses the function, and the same object persists across different calls to the function. Most of the time, this is not the behavior you want. In this case, however, we are taking advantage of this feature to store a timestamp from the previous call to the function, without having to use an external global variable. We borrowed this trick from our studies onclosures, a technique that we encourage you to read about.
After printing the message, we inspect the content of the only item intimestamp. If it isNone, we have no previous timestamp, so we set the value to the current time (#1). On the other hand, if we have a previous timestamp, we can calculate a difference (which we neatly format to three decimal digits), and finally, we put the current time intimestamp(#2).
Running this code outputs the following: