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.

7
  • 3
    um... if isTooLarge(101): print("too large"); break just let whatever calls isTooLarge deal with the error message/text. Commented Jul 29, 2016 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. Commented Jul 29, 2016 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. Commented Jul 29, 2016 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 stackoverflow.com/questions/431866/… Commented Jul 29, 2016 at 20:25
  • 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. Commented Jul 29, 2016 at 20:34

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.