0

When running a function, where H is some other function.

def f1(x):
    f1 = 5000 - H(x)
    if f1 >= 0:
        return f1 

if f1 < 0, then it returns None, but somewhere else in my program, I want to do something if the functions returns None. I know if it is a list then I can check if it is empty but I do not want it to return a list.

Thanks

I have no idea. I end up getting TypeError: '>=' not supported between instances of 'NoneType' and 'int'

which I do not understand.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 2
    `if f1(...) is None:` or `if f1(...) is not None:`. Note that this is one of relatively few places where `is` is the appropriate comparison operator. – jasonharper Nov 30 '22 at 17:54
  • A note: 99% of the time, if your function returns a useful value on success and `None` on failure, the better solution is to raise an exception on error; returning `None`, if not a clear part of the API behavior, risks an error going undetected for a while (until the `None` is used in a context where the expected type has behaviors `None` lacks), where an exception will be seen at the time of the error (and if it's not an error from the caller's perspective, they can just catch it to implement whatever default they feel is appropriate). – ShadowRanger Nov 30 '22 at 18:07

0 Answers0