[Python-ideas] PEP draft: context variables

Yury Selivanov yselivanov.ml at gmail.com
Tue Oct 10 12:29:54 EDT 2017


On Tue, Oct 10, 2017 at 12:21 PM, Koos Zevenhoven <k7hoven at gmail.com> wrote:
[..]
>> Please stop using "many things .. would", "most likely" etc.
>
>
> I can't explain everything, especially not in a single email. I will use
> whatever English words I need. You can also think for yourself––or ask a
> question.

I can't assign meaning to your examples formulated in "many things"
and "most likely".  I can reason about concrete words and code
examples.  You essentially asking us to *trust you* that you know of
some examples and they exist.  It's not going to happen.



>
>
>>
>> We have
>> a very focused discussion here.  If you know of any particular issue,
>> please demonstrate it with a realistic example.  Otherwise, we only
>> increase the number of emails and make things harder to track for
>> everybody.
>>
>
> I'm not going to (and won't be able to) list all those many use cases.

Then why are you working on a PEP? :)

[..]
>> The only such case
>> is contextlib.contextmanager, and PEP 550 provides mechanisms to make
>> generators "leaky" explicitly.
>>
>
> That's not the only one.
>
> Here's another example:
>
> def context_switcher():
>     for c in contexts:
>         decimal.setcontext(c)
>         yield
> ctx_switcher = context_switcher()
>
> def next_context():
>     next(ctx_switcher)

In 10 years of me professionally writing Python code, I've never seen
this pattern in any code base.  But even if such pattern exists, you
can simply decorate "context_switcher" generator to set it's
__logical_context__ to None. And it will start to leak things.

BTW, how does PEP 555 handle your own example?  I thought it's not
possible to implement "decimal.setcontext" with PEP 555 at all!


>
>
>
> And one more example:
>
>
> def make_things():
>     old_ctx = None
>     def first_things_first():
>         first = compute_first_value()
>         yield first
>
>         ctx = figure_out_context(first)
>         nonlocal old_ctx
>         old_ctx = decimal.getcontext()
>         decimal.setcontext(ctx)
>
>         yield get_second_value()
>
>     def the_bulk_of_things():
>         return get_bulk()
>
>     def last_but_not_least():
>         decimal.set_context(old_ctx)
>         yield "LAST"
>
>
>     yield from first_things_first()
>     yield from the_bulk_of_things()
>     yield from last_but_not_least()
>
> all_things = list(make_things())


I can only say that this one wouldn't pass my code review :)  This
isn't a real example, this is something that you clearly just a piece
of tangled convoluted code that you just invented.

Yury


More information about the Python-ideas mailing list