I came to python from Node.js and I'm accustomed to such error handling:
doSmth(function(err, data) {
if (err) {
log(err);
} else {
fooSmth(data, function(err, res) {
if (err) ...
})
}
})
Is it acceptable in Python to handle errors like
def do_smth(arg):
try:
data = int(arg)
return None, data
except Exception as e:
logger(e)
return e, None
err, data = do_smth("1715")
if not err:
print data
So that the function always return a tuple with exception and data values:
(None, data) or (Exception, None)
ExceptionwhenValueErrorwill do.