[Python-Dev] PEP 550 v4 (original) (raw)

Yury Selivanov yselivanov.ml at gmail.com
Thu Sep 7 02:43:42 EDT 2017


On Wed, Sep 6, 2017 at 11:39 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

Yury Selivanov wrote:

It would be great if you or Greg could show a couple of real-world examples showing the "issue" (with the current PEP 550 APIs/semantics). Here's one way that refactoring could trip you up. Start with this: async def foo(): calculatesomething() #in a coroutine, so we can be lazy and not use a cm ctx = decimal.getcontext().copy() ctx.prec = 5 decimal.setcontext(ctx) calculatesomethingelse() And factor part of it out (into an ordinary function!) async def foo(): calculatesomething() calculatesomethingelsewith5digits() def calculatesomethingelsewith5digits(): ctx = decimal.getcontext().copy() ctx.prec = 5 decimal.setcontext(ctx) calculatesomethingelse() Now we add some more calculation to the end of foo(): async def foo(): calculatesomething() calculatesomethingelsewith5digits() calculatemorestuff() Here we didn't intend calculatemorestuff() to be done with prec=5, but we forgot that calculatesomethingelse with5digits() changes the precision and *doesn't restore it* because we didn't add a context manager to it. If we hadn't been lazy and had used a context manager in the first place, that wouldn't have happened. Summary: I think that skipping context managers in some circumstances is a bad habit that shouldn't be encouraged. -- Greg


Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/yselivanov.ml%40gmail.com



More information about the Python-Dev mailing list