0
def check(ok, msg):
  if not ok:
    print msg

check(a = 1, "a=1 expected")
check(bugFile == None, "We still have a bugfile = " + bugFile)

我希望仅在bugFile != None. 合理吗?

4

5 回答 5

3

可能是你正在寻找这样的东西,使用str.format

def check(ok, msg, val):
  if not ok:
    print msg(val)

check(bugFile is None, "We still have a bugfile = {}".format, bugFile)

演示:

>>> bugFile = None
>>> check(bugFile is None, "We still have a bugfile = {}".format, bugFile)
>>> bugFile = 100
>>> check(bugFile is None, "We still have a bugfile = {}".format, bugFile)
We still have a bugfile = 100

另一种选择可能是functools.partial,这里不需要传递额外的 val参数:

from functools import partial
def check(ok, msg):
  if not ok:
    print msg()

bugFile = None
check(bugFile is None, partial("We still have a bugfile = {}".format, bugFile))
bugFile = 100
check(bugFile is None, partial("We still have a bugfile = {}".format, bugFile))
于 2013-11-06T15:09:49.720 回答
2

对于这种特定情况,很容易解决:

def check(ok, msg, msg_args=()):
  if not ok:
    print msg % msg_args

check(a == 1, "a=1 expected")
check(bugFile == None, "We still have a bugfile = %s", bugFile)

然而,一般来说,延迟计算可能并不容易。在最坏的情况下,您可以使用匿名函数(lambdas):

def check(ok, msg_f):
  if not ok:
    print msg_f()

check(a == 1, lambda: "a=1 expected")
check(bugFile == None, lambda : "We still have a bugfile = %s" % bugFile)

如果您对延迟评估感兴趣,您可能还想查看lazypy 。

最后,该%运算符已弃用,因此您可能希望改用str.format

于 2013-11-06T15:11:36.463 回答
1

听起来您正在尝试使用assert

>>> a = 2
>>> assert a == 1, "a == 1 expected"

Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    assert a == 1, "a == 1 expected"
AssertionError: a == 1 expected

如您所见,什么时候a不是1它会引发异常。

>>> bugfile = None
>>> assert bugfile == None, "We still have a bugfile = " + bugfile

如您所见,什么时候bugfile什么None都不做。

>>> bugfile = 'omg! a bug'
>>> assert bugfile == None, "We still have a bugfile = " + bugfile

Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    assert bugfile == None, "We still have a bugfile = " + bugfile
AssertionError: We still have a bugfile = omg! a bug

当它某物时,它会引发异常!


如果您担心异常,请尝试以下操作:

>>> bugfile = 'omg! a bug'
>>> if not bugfile == None: print "We still have a bugfile = " + bugfile

We still have a bugfile = omg! a bug # as you can see, it printed.

>>> bugfile = None
>>> if not bugfile == None: print "We still have a bugfile = " + bugfile

>>> # everything okay
于 2013-11-06T15:11:28.173 回答
0

如果总是要定义 bugFile,那么以下内容应该就是您所需要的。

if bugFile != None:
    print "We still have a bugfile = " + bugFile

如果您不知道是否定义了 bugFile 变量,那么您可以尝试触发 Python 在尝试读取未定义的变量时抛出的 NameError,然后用异常捕获它。

try:
    if bugFile != None:
       print "We still have a bugfile = " + bugFile
    else:
       print "bugFile is None"
except NameError:
    print "bugFile is not defined at all"

如果可以避免,请不要做第二个。你将来会后悔的。

于 2013-11-06T15:11:42.977 回答
0

如果您想开始使用 lambda 函数,您实际上可以在 python3 中打印,这也可以:

bugfile = "something"
output = lambda x: print("We still have a bugfile {0}".format(x)) if x else print("Bug is gone")

>>>output(bugfile)
>>>"We still have a bugfile something"
>>>bugfile = ""
>>>output(bugfile)
>>>"Bug is gone"
于 2013-11-06T15:36:39.893 回答