1

Current situation:

def isTooLarge(intValue):
  if intValue > 100: print "too large"; return True
  return False

if isTooLarge(101): break

Now I like to make the function more "library-friendly" by returning the errormessagetext instead of printing it:

def isTooLarge(intValue):
  if intValue > 100: return True, "too large"
  return False

bln,str = isTooLarge(101)
if bln: specialprint(str); break

Any idea how I can evaluate it as an one-liner again? (something like "if ,str isTooLarge(101): specialprint(str); break" or what is the Python way here?

No problem to put the errormessage into a global variable like "lasterrormessage" and keep the rest as is.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 3
    um... `if isTooLarge(101): print("too large"); break` just let whatever calls `isTooLarge` deal with the error message/text. – Tadhg McDonald-Jensen Jul 29 '16 at 20:23
  • As far as I know, there is not really anything like you're looking for without evaluating `isTooLarge` twice. But what you have written is probably the clearest way. – Will Molter Jul 29 '16 at 20:24
  • 1
    Otherwise have two functions, one that just returns True or False and another one that calls the first but has extra logic if it returns True. Quite frankly I'm not sure what you are trying to accomplish with this error text thing. – Tadhg McDonald-Jensen Jul 29 '16 at 20:25
  • Try `isTooLarge(101)[0]`. Caveat, I'm not a Python guy, so if it doesn't work, you've been warned. See http://stackoverflow.com/questions/431866/ignore-python-multiple-return-value – chessofnerd Jul 29 '16 at 20:25
  • Why don't you just return either the text or False? Non-empty strings are truthy. – L3viathan Jul 29 '16 at 20:32
  • 2
    It almost sounds like you should be throwing an exception. For example, `raise ValueError("too large")`. Then, the callie has to deal with it. – rkersh Jul 29 '16 at 20:34
  • @Will: thanks. I guessed it and will think about something else. The error message is a hint mostly used for debugging, maybe I will do something with a "debugoutput-class" which can be overwritten by the callee. – Bernd Hohmann Jul 30 '16 at 17:05

2 Answers2

2

You can be much more "library friendly" by using errors like they are meant for, as errors:

def CheckNotTooLarge(intValue):
    if intValue > 100:
        raise ValueError("too large") #or AssertionError
    return #or maybe do something else?

then a user could use the error message completely separately using try: except:

try:
   CheckNotTooLarge(101)
except ValueError:
    traceback.print_exc() #print error message
    #handle too large
else:
    #handle not too large

I can see how this would quickly get annoying if you just want to check without handling errors so I'd recommend having two functions, one that just returns a boolean, no extra work and another that raises/returns the error text:

def isTooLarge(intValue):
    return intValue<=100 #now this is a one liner!

def checkIsTooLarge(intValue):
    "uses isTooLarge to return an error text if the number is too large"
    if isTooLarge(intValue):
       return "too large" #or raise ...
    else:
       return False
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • I agree that a ValueError would be more appropriate for this kind of check although when the function name is `assert___` I'd probably expect it to raise an `AssertionError`. – Tadhg McDonald-Jensen Jul 29 '16 at 21:05
  • For the purposes of this example, I'd change the name of that function to `dosomething` and replace the `return False` with `# do something here`. Assertions are a different topic, in my opinion. At any rate, it was a minor quibble. – rkersh Jul 29 '16 at 21:13
  • but @rkersh the function in the question doesn't do anything else... Whatever, ValueError is definitely the right Exception type to use and comments added where possibilities exist. – Tadhg McDonald-Jensen Jul 29 '16 at 21:29
  • Thanks a lot for the suggestion. I just forgot about the existence of "Exceptions" in Python. On the other hand I'm using them usually for real, exceptional errors (eg. unexpected EOF, socket close) and try to avoid them for flowcontrol because they are costly for the CPU and difficult to read (at least in Java). – Bernd Hohmann Jul 30 '16 at 17:00
0
def isTooLarge(intValue):
    return "too large" if intValue > 100 else False

x = isTooLarge(101); x and print(x)

However. Please don't do this. Exceptions exist for a reason. Putting things in one line simply for the sake of it makes your code hard to read.

L3viathan
  • 26,748
  • 2
  • 58
  • 81