Nathaniel Smith wrote:
Literally the first motivating example at the beginning of the PEP
('def fractions ...') involves only generators, not coroutines, and
only works correctly if generators get special handling. (In fact, I'd
be curious to see how Greg's {push,pop}_local_storage could handle
this case.)

I've given a decimal-based example, but it was a bit
scattered. Here's a summary and application to the
fractions example.

I'm going to assume that the decimal module has been
modified to keep the current context in a context var,
and that getcontext() and setcontext() access that
context var.

THe decimal.localcontext context manager is also
redefined as:

   class localcontext():

      def __enter__(self):
         push_local_context()
         ctx = getcontext().copy()
         setcontext(ctx)
         return ctx

      def __exit__(self):
         pop_local_context()

Now we can write the fractions generator as:

   def fractions(precision, x, y):
      with decimal.localcontext() as ctx:
        ctx.prec = precision
        yield Decimal(x) / Decimal(y)
        yield Decimal(x) / Decimal(y ** 2)

You may notice that this is exactly the same as
what you would write today for the same task...

--
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

Reply via email to