使用datetime的python默认参数值 [英] python default parameter value using datetime

查看:53
本文介绍了使用datetime的python默认参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Python脚本,该脚本使用Ctl-C的信号处理程序.如果程序正常完成,则将结束时间传递给"print_results"函数.我希望print_results函数具有一个可选参数,如果未传递该参数,则仅获取当前的现在"时间.但是,当我从信号处理程序调用它时,它没有得到正确的时间.

I have a simple Python script that uses a signal handler for Ctl-C. If the program completes normally, the end time is passed into the "print_results" function. I wanted the print_results function to have an optional parameter that, if not passed, simply gets the current "now" time. But when I call it from the signal handler, it does not get the correct time.

这是我简化但可复制的程序:

Here is my simplified, but reproducible, program:

import sys
import signal
import urllib2
import urllib
import datetime
import time
import getopt,sys

def signal_handler(signal, frame):
    print_results()
    sys.exit(0)

def print_results(ended=datetime.datetime.now()):
    print "\nEnded at ",ended
    print "Total time: ",(ended - startTime)
    print "Finished ",numIterations," iterations, received ",totalRecords," records"

    numIterations = 0
    maxIterations = 8
    delaySecs = 3
    totalRecords = 0

    # set up signal handler
    signal.signal(signal.SIGINT, signal_handler)

    startTime = datetime.datetime.now()

    print "Starting at ",time.asctime(time.localtime())

    while (numIterations < maxIterations):

        iterStartTime = datetime.datetime.now()

        numIterations += 1

        print "Iteration: ",numIterations

        # sleep if necessary

        if delaySecs > 0:
            time.sleep(delaySecs)

        iterEndTime = datetime.datetime.now()

        print "Iteration time: ",(iterEndTime - iterStartTime)

    endTime = datetime.datetime.now()

    print "Ended at ",time.asctime(time.localtime())
    print "Total test time: ",(endTime - startTime)

    print_results(endTime)

这是我输入Ctl-C时发生的事情

Here is what happens when I type Ctl-C

$ python test.py                                                           
Starting at  Fri Jun 15 08:28:15 2012
Iteration:  1
Iteration time:  0:00:03.003101
Iteration:  2
Iteration time:  0:00:03.003105
Iteration:  3
^C
Ended at  2012-06-15 08:28:15.766496
Total time:  -1 day, 23:59:59.999964
Finished  3  iterations, received  0  records

似乎在不带参数的情况下调用print_results时,"end"值未正确解释为日期时间对象.但是自从Python没有办法(据我所知),我无法分辨出什么地方出了错.

It seems like that when print_results is called with no arguments that the 'ended' value is not being interpreted correctly as a datetime object. But since Python does not have a way to cast (as far as I can tell), I cannot tell what is wrong.

预先感谢

米奇

推荐答案

您遇到的问题是您正在评估参数中的函数.这意味着 ended = datetime.datetime.now()将采用解析时的时间值,而不是调用时的时间值.您应该做的是这样的:

The problem you are having is that you are evaluating the function in the parameter. This means that ended=datetime.datetime.now() takes the value of the time when this is being parsed, not when it is called. What you should do is something like this:

def print_results(ended=None):
    if ended is None:
        ended = datetime.datetime.now()
    ...

这里有一个很好的解释为什么会发生这种情况:"Python中的最少惊讶":可变默认参数

Here there is a really good explanation of why this happens: "Least Astonishment" in Python: The Mutable Default Argument

这篇关于使用datetime的python默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆