我刚开始使用Python,也许我担心得太快了,但是无论如何。。。在log = "/tmp/trefnoc.log"
def logThis (text, display=""):
msg = str(now.strftime("%Y-%m-%d %H:%M")) + " TREfNOC: " + text
if display != None:
print msg + display
logfile = open(log, "a")
logfile.write(msg + "\n")
logfile.close()
return msg
def logThisAndExit (text, display=""):
msg = logThis(text, display=None)
sys.exit(msg + display)
这很管用,但我不喜欢它的样子。有没有更好的方法来编写这个(也许只有一个函数),在exiting下我是否应该关注any other thing?在
现在是一些背景(但不是关于trefnoc)。。。在
有时我会调用logThis来记录和显示。其他时候我想打电话离开。最初我是这样做的:
^{pr2}$
然后我认为这不能正确地设置stderr,因此当前代码显示在顶部。在
我的第一个想法实际上是通过了”系统出口“作为一个参数,并定义如下定义的logThis ("ERROR. EXITING", call=sys.exit)(仅显示相关的差异部分):def logThis (text, display="", call=print):
msg = str(now.strftime("%Y-%m-%d %H:%M")) + " TREfNOC: " + text
call msg + display
但这显然行不通。我认为Python不会在变量中存储函数。我无法(快速地)找到Python是否可以有带函数的变量!可能使用eval函数?我真的总是试着避开他们。当然,我想用if代替另一个def,但这不会是更好或最坏的。在
不管怎样,有什么想法吗?在