Question

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

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

I want the latter string be evaluated only when bugFile != None. Is it reasonable?

Was it helpful?

Solution 2

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

OTHER TIPS

May be you're looking for something like this, using str.format:

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

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

Demo:

>>> 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

Another option could be functools.partial, here no need to pass the extra val parameter:

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))

Sounds like you are trying to use 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

As you can see, when a is not 1 it throws an exception.

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

As you can see, when bugfile is None it does nothing.

>>> 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

And when it is something, it throws an exception!


If you are worried about the exceptions, try this:

>>> 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

If bugFile is always going to be defined, then the below should be all you need.

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

If you don't know if the bugFile variable is defined then you can try to trigger the NameError that python throws when it tries to read a variable that isn't defined and then catch it with an except.

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"

Please don't do the second one if you can avoid it. You will regret it in the future.

If you wanted to start using lambda functions, you can actually print in python3 this would also work:

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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top