For this specific case, it's easy enough to solve:
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)
In general, however, it might not be this easy to delay the computation. In the worst case scenario, you can use anonymous functions (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)
You may also want to check out lazypy if you're interested in lazy evaluation.
Finally, the %
operator is deprecated, so you may want to use str.format instead