A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20180103/ab0784a0/attachment.html below:

<div dir="auto"><div style="font-family:sans-serif" dir="auto">> Do you have any use case for modifying a variable inside some context?</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">> numpy, decimal, or some sort of tracing for http requests or async frameworks like asyncio do not need that.</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">Maybe I misunderstood how contextvars is supposed to be used. So let me give you an example.</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">I understand that decimal.py will declare its context variable like this:</div><div style="font-family:sans-serif" dir="auto">---</div><div style="font-family:sans-serif" dir="auto">contextvar = contextvars.ContextVar('decimal', default=Context(...))</div><div style="font-family:sans-serif" dir="auto">---</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">Later if I would like to run an asyncio callback with a different decimal context, I would like to write:</div><div style="font-family:sans-serif" dir="auto">---</div><div style="font-family:sans-serif" dir="auto">cb_context = contextvars.copy_context()</div><div style="font-family:sans-serif" dir="auto">decimal_context = cb_context[decimal.contextvar].copy()</div><div style="font-family:sans-serif" dir="auto">decimal_context.prec = 100</div><div style="font-family:sans-serif" dir="auto">cb_context[decimal.contextvar] = decimal_context # <--- HERE</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">loop.call_soon(func, context=cb_context)</div><div style="font-family:sans-serif" dir="auto">----</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">The overall code would behaves as:</div><div style="font-family:sans-serif" dir="auto">---</div><div style="" dir="auto"><font face="sans-serif">with localcontext() as ctx:</font></div><div style="" dir="auto"><font face="sans-serif">  ctx.prec = 100</font></div><div style="" dir="auto"><font face="sans-serif">  loop.call_soon(func)</font></div><div style="" dir="auto"><font face="sans-serif">---</font></div><div style="" dir="auto"><font face="sans-serif"><br></font></div><div style="" dir="auto"><font face="sans-serif">I don't know if the two code snippets have exactly the same behaviour.</font></div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">I don't want to modify func() to run it with a different decimal context. So I would prefer to not have to call decimal.contextvar.set() or decimal.setcontext() in func().</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">But I would need contextvars.Context[var]=value to support such use case.</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">Decimal contexts are mutable, so modifying directly the decimal context object would impact all contexts which isn't my intent here.</div><div style="font-family:sans-serif" dir="auto"><br></div><div style="font-family:sans-serif" dir="auto">Victor</div></div>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4