[Python-Dev] PEP 567 v2

Victor Stinner victor.stinner at gmail.com
Fri Jan 5 05:05:18 EST 2018


Currently, Context.get(var) returns None when "var in context" is false.
That's surprising and different than var.get(), especially when var has a
default value.

Code:
---
import contextvars

name = contextvars.ContextVar('name', default='victor')
context = contextvars.copy_context()
print(name in context)
print(context.get(name))
print(name.get())
---

Output:
---
False
None
victor
---

Context.get() must raise a lookup error by default if var is not in
context. It should return the default argument if it's set, it's just that
the default parameter must not have a default value (None).

I'l fine that Context.get(default=None) and var.get() behaves differently
(return None vs victor in my example) when var isn't set and var has a
default value.

Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180105/0ae99702/attachment.html>


More information about the Python-Dev mailing list