2

I have been just wondering if it is possible to somehow combine 'if' and 'as' statements like this:

if possible_error() as error:
  return error

instead of

error = possible_error()
if error:
  return error

where 'possible_error' function returns either empty string or error message. By doing so I could save one line of code.

I know that some compromise is to run this function twice:

if possible_error():
  return possible_error()

but I would rather avoid doing this.

Fly_37
  • 182
  • 10

1 Answers1

5

This is what the relatively new "walrus" operator is for:

if (error := possible_error()):
    return error
Tim Roberts
  • 34,376
  • 3
  • 17
  • 24