
You can only modify a context when it's the current context, so using ContextVar.set() in Context.run(). Victor Le 5 janv. 2018 11:42 AM, "Paul Moore" <p.f.moore@gmail.com> a écrit : On 4 January 2018 at 23:58, Guido van Rossum <guido@python.org> wrote:
On Thu, Jan 4, 2018 at 9:27 AM, Paul Moore <p.f.moore@gmail.com> wrote:
On 4 January 2018 at 15:56, Guido van Rossum <guido@python.org> wrote:
It was get_context() in an earlier version of PEP 567. We changed it to copy_context() believing that that would clarify that you get a clone that is unaffected by subsequent ContextVar.set() operations (which affect the *current* context rather than the copy you just got).
Ah thanks. In which case, simply changing the emphasis to avoid the implication that Context objects are immutable (while that may be true in a technical/implementation sense, it's not really true in a design sense if ContextVar.set modifies the value of a variable in a context) is probably sufficient.
Do you have a specific proposal for a wording change? PEP 567 describes Context as "a read-only mapping, implemented using an immutable dictionary." This sounds all right to me -- "read-only" is weaker than "immutable". Maybe the implementation should not be mentioned here? (The crux here is that a given Context acts as a variable referencing an immutable dict -- but it may reference different immutable dicts at different times.)
I've been struggling to think of good alternative wordings (it's a case of "I'm not sure what you're trying to say, so I can't work out how you should say it", unfortunately). The best I can come up with is """ A Context is a mapping from ContextVar objects to their values. The Context itself exposes the Mapping interface, so cannot be modified directly - to modify the value associated with a variable you need to use the ContextVar.set() method. """ Does that explain things correctly? One thing I am sure of is that we should remove "implemented using an immutable dictionary" - it's an implementation detail, and adds nothing but confusion to mention it here. Paul _______________________________________________ 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/ victor.stinner%40gmail.com

On 5 January 2018 at 11:06, Victor Stinner <victor.stinner@gmail.com> wrote:
Le 5 janv. 2018 11:42 AM, "Paul Moore" <p.f.moore@gmail.com> a écrit : """ A Context is a mapping from ContextVar objects to their values. The Context itself exposes the Mapping interface, so cannot be modified directly - to modify the value associated with a variable you need to use the ContextVar.set() method. """
Does that explain things correctly? One thing I am sure of is that we should remove "implemented using an immutable dictionary" - it's an implementation detail, and adds nothing but confusion to mention it here.
You can only modify a context when it's the current context, so using ContextVar.set() in Context.run().
Thanks. That's a useful qualification. But it may be too detailed for the summary - it's certainly something that should be covered in the specification section, though. Maybe "... you need to use the ContextVar.set() method from Context.run()" would be OK, although I don't want the summary to get too long. Paul

On Fri, Jan 5, 2018 at 3:10 AM, Paul Moore <p.f.moore@gmail.com> wrote:
On 5 January 2018 at 11:06, Victor Stinner <victor.stinner@gmail.com> wrote:
Le 5 janv. 2018 11:42 AM, "Paul Moore" <p.f.moore@gmail.com> a écrit : """ A Context is a mapping from ContextVar objects to their values. The Context itself exposes the Mapping interface, so cannot be modified directly - to modify the value associated with a variable you need to use the ContextVar.set() method. """
Does that explain things correctly? One thing I am sure of is that we should remove "implemented using an immutable dictionary" - it's an implementation detail, and adds nothing but confusion to mention it here.
You can only modify a context when it's the current context, so using ContextVar.set() in Context.run().
Thanks. That's a useful qualification. But it may be too detailed for the summary - it's certainly something that should be covered in the specification section, though. Maybe "... you need to use the ContextVar.set() method from Context.run()" would be OK, although I don't want the summary to get too long.
(Sorry, I wrote my previous response before seeing this part of the exchange.) Maybe "... you need to use the ContextVar.set() method, which modifies the current context." Then we also need to update the summary for ContextVar.set(), which currently IMO is plain wrong. -- --Guido van Rossum (python.org/~guido)
participants (3)
-
Guido van Rossum
-
Paul Moore
-
Victor Stinner