tags:

views:

79

answers:

4

is there a function that will return true if some_function returns an error?

+4  A: 

No. Python uses exceptions to handle errors, and these are caught using try...catch blocks.

In other words, what is your definition of an error? False? Null? ""?

Edit:

To answer your question in the comment, there is no well-defined way of comparing, say, "1.1" and "1.2". This is intentional, because there is no obvious ordering on strings. Now in fact, Python does support comparing strings, but it uses lexicographic (alphabetical) ordering, which isn't what you want. This is because these strings are semantically floats -- they represent numbers. Now the real problem is that they shouldn't have been strings in the first place (because they're numbers!), but sometimes you can't fix that. So instead, turn them into numbers:

float(1.1) > float(1.2)
katrielalex
ok so the problem: how do i compare two strings. for example how a=1.1 and b=1.2, how do i know which one is greater?
I__
Define "greater" in terms of strings. Is "200" greater than "1000"? Is "999" greater than "Zackary"?
Lasse V. Karlsen
You need to cast them both to numbers, in this case `float` s, because there's no way of comparing strings. Do `float(a) > float(b)`.
katrielalex
You *can* compare strings lexicographically with the regular comparison operators. As @Lasse said, it depends what kind of comparison you want.
Matthew Flaschen
Sorry -- you know what I meant =p! Have elaborated in the post.
katrielalex
@katriealex: How do you know what the OP wants/needs? His data may be paragraph numbers limited to "\d\.\d". Who says they are "semantically float"?
John Machin
OK, so I generalised a little bit too much; based on the post history I don't s/he's doing anything very complicated. I guess it would be more correct to say that they are semantically `decimal` but that this is best represented in a builtin by `float`.
katrielalex
+2  A: 

In python you do error checking with exceptions:

try:
    some_function()
except Exception:
    print "error"

You need to define some_function() to raise Exception if an error happened.

Ian Wetherbee
+2  A: 

No you do try..except ErrorType as e: and then if you enter the exception handler, you know that the type you chose error happened. Built-in Exceptions

To compare two strings containing both numbers in same amount of decimals you can do:

print ("%20s" % first) >= ("%20s" % second)

More general way is to do normal comparison but for equality use absolute value of difference comparison.

numbers= ("1.2","1.3")
a,b= (float(num) for num in numbers)
print("Bigger or equal" if a>b else "Smaller")
Tony Veijalainen
A: 

You should add the kind of error you are expecting after except, but leaving it as is will work as well.

def isError()
    try:
        some_function
    except Exception:
        return True
Zonda333
-1 You have the strangest notion of "work" that I've ever seen. **A bare except is EVIL.**
John Machin
Did you not read my answer? I said to add the exception, but didn't put it in the code as I don't know what errors some_function could return. Updated to be more clear.
Zonda333
Yes -- I read your answer. It said in effect that a bare except would work.
John Machin
Bare excepts do work, they are just aren't good.
Zonda333