Is there a way in python to turn a try/except into a single line?
something like...
b = \'some variable\'
a = c | b #try statement goes here
<
The problem is that its actually a django model.objects.get query i am trying to test. the .get returns an error if no data is found... it doesn't return None (which annoys me)
Use something like this:
print("result:", try_or(lambda: model.objects.get(), ''))
Where try_or is an utility function defined by you:
def try_or(fn, default):
try:
return fn()
except:
return default
Optionally you can restrict the accepted exception types to NameError, AttributeError, etc.