[Python-ideas] Type hints for functions with side-effects and for functions raising exceptions

Chris Angelico rosuav at gmail.com
Fri Feb 22 13:54:48 EST 2019


On Sat, Feb 23, 2019 at 5:32 AM Juancarlo Añez <apalala at gmail.com> wrote:
> On Thu, Feb 21, 2019 at 11:32 PM Chris Angelico <rosuav at gmail.com> wrote:
>>
>> On Fri, Feb 22, 2019 at 2:27 PM Juancarlo Añez <apalala at gmail.com> wrote:
>> > Then, if exceptions are going to be part of a type, there should be a way to express the semantics of them (like in Eiffel), so stack.pop();stack.push(x) doesn't have to catch StackFullException.
>> >
>>
>> That assumes atomicity. If you want an atomic "replace top of stack"
>> that can never raise StackFullException, it's probably best to express
>> it as stack.replacetop(x) rather than having something that might be
>> interrupted.
>
> Ah! What to do with an exception in a concurrent context? Abort, or retry after a while?

You mean in a situation where another thread might be pushing/popping
on the same stack? That's up to you. Exception handling works exactly
the same way with multiple threads as it does with a single thread;
each context has a call stack.

Things are a bit more complicated with other forms of concurrency, but
at the point where there is an execution context again, that's where
exceptions can start bubbling again. That's how generators work, for
instance.

ChrisA


More information about the Python-ideas mailing list