On 09/07/2017 03:37 AM, Greg Ewing wrote:

If I understand correctly, instead of using a context
manager, your fractions example could be written like
this:

def fractions(precision, x, y):
     ctx = decimal.getcontext().copy()
     decimal.setcontext(ctx)
     ctx.prec = precision
     yield MyDecimal(x) / MyDecimal(y)
     yield MyDecimal(x) / MyDecimal(y ** 2)

and it would work without leaking changes to the decimal
context, despite the fact that it doesn't use a context
manager or do anything else to explicitly put back the
old context.

The disagreement seems to be whether a LogicalContext should be created implicitly vs explicitly (or opt-out vs opt-in). As a user trying to track down a decimal context change not propagating, I would not suspect the above code of automatically creating a LogicalContext and isolating the change, whereas Greg's context manager version is abundantly clear.

The implicit vs explicit argument comes down, I think, to resource management: some resources in Python are automatically managed (memory), and some are not (files) -- which type should LCs be?

--
~Ethan~

_______________________________________________
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

Reply via email to