ramazan polat
ramazan polat

Reputation: 7900

if error, do this and return, else continue execution in one line

There are lots of repetitions in my code like this block:

if err:
    print('Could not parse text!')
    print('Error code={}'.format(err.code))
    print('Error message={}'.format(err.message))
    return err.code

I want to make it look nicer, maybe in just one line of code.

So I want to order the compiler to do this in one line:

if there is an error, print necessary information and return error code, otherwise continue execution.

Something like this:

def error_output(err, text):
    print(text)
    print('Error code={}'.format(err.code))
    print('Error message={}'.format(err.message))
    return err.code

return_if(err, error_output, 'Parse error')

Tried this:

return error_output(err,'parse error') if err else continue

But of course it's not possible to use continue like this.

Upvotes: 1

Views: 639

Answers (1)

Mike Müller
Mike Müller

Reputation: 85492

How about:

if err: return error_output(err, 'parse error') 
# more code here

Upvotes: 3

Related Questions