[Python-ideas] PEP draft: context variables
Neil Girdhar
mistersheik at gmail.com
Tue Sep 5 17:09:37 EDT 2017
On Tue, Sep 5, 2017 at 10:54 AM Guido van Rossum <guido at python.org> wrote:
> On Tue, Sep 5, 2017 at 7:42 AM, Neil Girdhar <mistersheik at gmail.com>
> wrote:
>
>> I think you really should add a context manager to PEP 550 since it is
>> better than calling "set", which leaks state. Nathaniel is right that you
>> need set to support legacy numpy methods like seterr. Had there been a way
>> of setting context variables using a context manager, then numpy would only
>> have had to implement the "errstate" context manager on top of it. There
>> would have been no need for seterr, which leaks state between code blocks
>> and is error-prone.
>>
>
> There is nothing in current Python to prevent numpy to use a context
> manager for seterr; it's easy enough to write your own context manager that
> saves and restores thread-local state (decimal shows how). In fact with PEP
> 550 it's so easy that it's really not necessary for the PEP to define this
> as a separate API -- whoever needs it can just write their own.
>
Don't you want to encourage people to use the context manager form and
discourage calls to set/discard?
I recognize that seterr has to be supported and has to sit on top of some
method in the execution context. However, if we were starting from
scratch, I don't see why we would have seterr at all. We should just have
errstate. seterr can leak state, which might not seem like a big deal in
a small program, but in a large program, it can mean that a minor change in
one module can cause bugs in a totally different part of the program.
These kinds of bugs can be very hard to debug.
> --
> --Guido van Rossum (python.org/~guido)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170905/06611d3a/attachment.html>
More information about the Python-ideas
mailing list