Steven D'Aprano wrote:
result = computation(
int(arg) except ValueError: abort("Invalid int")
)
Actually, not quite so nice as I first thought, since you're relying on
the side-effects of abort() rather than returning a value.
Yeah, while I was writing that I wondered whether
you should be allowed to write
int(arg) except ValueError: raise UserError("Invalid int")
That looks heretical, because 'raise' can't in any
way be interpreted as a value-returning expression.
But you can achieve the same result using a function
that always raises and exception, so forbidding it
on those grounds would be pointless.
And writing it that way at least makes it obvious that
it *does* always raise an exception, in the same way
that
try:
i = int(arg)
except ValueError:
raise UserError("Invalid int")
else:
result = computation(i)
makes it obvious that control can't fall off the
end of the except branch.
--
Greg
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com