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
. 合理吗?
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
. 合理吗?
可能是你正在寻找这样的东西,使用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))
对于这种特定情况,很容易解决:
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
听起来您正在尝试使用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
如果总是要定义 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"
如果可以避免,请不要做第二个。你将来会后悔的。
如果您想开始使用 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"