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)

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

Val
  • 1
  • 8
  • 40
  • 64
  • You're aware that you're printing the string only when `bugFile` evaluates to false? – aIKid Nov 06 '13 at 15:06
  • 1
    Mate, that's just made things worse. :/ – wdh Nov 06 '13 at 15:09
  • 1
    The confusion is that you said you want to check whether `bugFile` is _defined_. Truth is though, you want to see whether or not it is `None`. There is a difference. –  Nov 06 '13 at 15:10
  • `not ok` will be `False` if ok is `True`. *Not* `None`. Is it clear enough? – aIKid Nov 06 '13 at 15:11
  • That's the confusion, a variable that is == None *is* defined, it is just None. The term 'defined variable' has a very precise meaning, it means a variable that has a value. Even if that value is None, it is still defined. It is just defined to be None. – wdh Nov 06 '13 at 15:12
  • This already exists in Python, its called [`assert`](http://docs.python.org/2/reference/simple_stmts.html#the-assert-statement). Check [my answer](http://stackoverflow.com/a/19815679/1561176). – Inbar Rose Nov 06 '13 at 15:15

5 Answers5

3

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))
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
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

Community
  • 1
  • 1
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
  • I tried the comma and got `heck() takes exactly 2 arguments (3 given)`. I do not like to define lambda every time. – Val Nov 06 '13 at 15:16
  • @Val you need to redeclare the `check` function. Notice I added an argument to it – loopbackbee Nov 06 '13 at 15:18
  • @Val [Better use `functools.partial`](http://stackoverflow.com/questions/3252228/python-why-is-functools-partial-necessary), check my solution. – Ashwini Chaudhary Nov 06 '13 at 15:20
  • I do not see how it is better. You create a dependence on one more package and, secondly, demand wrapping every call with partial. I do not see how it is better than lambda. – Val Nov 06 '13 at 15:23
  • @Val Did you check [this link](http://stackoverflow.com/questions/3252228/python-why-is-functools-partial-necessary)? If you don't want to use the built-in modules then you're using a wrong language. – Ashwini Chaudhary Nov 06 '13 at 15:27
1

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
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • Yes, I am asserting. I created my own for more flexibility. But, you are right, I may capture it in exception handler. – Val Nov 06 '13 at 15:18
0

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.

wdh
  • 1,612
  • 12
  • 16
  • I have defined the check function because I do not like to copy-paste `if-error_report` every time. – Val Nov 06 '13 at 15:15
0

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"
jwillis0720
  • 4,329
  • 8
  • 41
  • 74