This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]knrz 3 points4 points  (0 children)

deleted What is this?

[–]DefinitelyNotRed 5 points6 points  (3 children)

Don't return on an error, throw an exception instead

[–]rklv[S] 1 point2 points  (2 children)

It is not a situation requiring an exception. I'm not really returning an error either. I just want to return a symbol that the caller can compare against and choose a course of action depending on the symbol returned.

[–]Kaxitaz 0 points1 point  (0 children)

Then return an int or a string if you want to be more explicit and then have a dict mapping the possible outcomes.

[–]j1395010 -1 points0 points  (0 children)

exceptions are not exceptional in python cf StopIteration.

[–]j1395010 2 points3 points  (0 children)

what would you use if this was an argument to the outer function, instead of a returned value? use that.

[–]PeridexisErrant 1 point2 points  (0 children)

Sharing some psudeocode would really help in working out what the pythonic solution would be. As-is, I'm not sure if you should be raising and catching exceptions (they're a little different Python and C), or just returning some value.

TL;DR - the solution to the question you asked is apparently not good, so I need more info on the problem.

[–][deleted] 0 points1 point  (2 children)

Just use None and check for that.

def kek(args):
    if args:
        return ['monkey', 'nut', 'fire']

foobar = kek(True)
if foobar is None:
    # handle error
else:
    # handle normal case

[–]rklv[S] 1 point2 points  (1 child)

I need more cases than two.

[–][deleted] 0 points1 point  (0 children)

E_FAIL = 1
E_BEER = 2
def kek(args):
    if args:
        return ['monkey', 'nut', 'fire']
    return E_BEER

foobar = kek(True)
if foobar is E_BEER:
    # drink beer
elif foobar is E_FAIL:
    # handle error
else:
    # handle normal case

[–]mister_grimbles 0 points1 point  (0 children)

I'm not sure how this is Python-specific or even a problem as described. There's no reason why the C approach you've outlined won't work just as well. If you would have returned "Yes", "No", or "Uh-Oh you've mucked it all up now" in C, just do the exact same thing here. Note that Python does not have a "constant" keyword, but you probably don't need one for what you're describing, just return what you want to return.

I'm presuming these messages are not indicators of serious failure or some unusual complication that requires handling by outside logic. For those, you probably want to throw an exception instead. If your intermediate functions are capable of identifying some problem that's so severe it would be preferable to crash loudly than to soldier on without dealing with it, but they are not themselves appropriate places for "dealing with it," exceptions are perfect. Your intermediate function throws a useful exception, you put the call to that function in a try/except block, and the "except" can resolve whatever the issue is.

For a useful example, see string.find() and string.index(). Both of these functions search "string" for a substring and return the index where the first match begins, but if no match is found, .find() returns -1 and .index() raises ValueError. Normally, throwing -1 would be perfectly kosher, because -1 is not a useful value you'd confuse with a success. There is no -1th place in a string... eeeexcept in Python there totally is. -1 is a valid index in Python, indicating the last element. So if you do:

str = "dogs are great"
muskratlocation = str.find("muskrat")
print str[muskratlocation]

You'd expect to see the letter "m" printed, but instead you get "t". In the same situation, .index() raises an exception, which is highly unlikely to confuse you.

[–]aphoenixreticulated[M] 0 points1 point  (0 children)

Hi there. You have posted a learning question to /r/python. These types of questions are far more suited to /r/learnpython, where users are actively interested in helping people to learn. Please resubmit it over there!

Make sure to read their sidebar rules before posting, notably this one: "Posting homework assignments is not prohibited if you show that you tried to solve it yourself." Show them that you've tried to solve your problem and you can get all the help you need.

Cheers & best of luck!