[Python-ideas] PEP 505: None-aware operators: operators ?= and ?? and OR

MRAB python at mrabarnett.plus.com
Thu Aug 2 16:19:10 EDT 2018


On 2018-08-02 20:03, Eric Fahlgren wrote:
>  From the PEP:
>  >
>  From email/generator.py (and importantly note that there is no way to 
> substitute or for ?? in this situation):
>  >
> mangle_from_ = True if policy is None else policy.mangle_from_
>  > After updating:
>  >
> mangle_from_ =
> policy?.mangle_from_ ?? True
> 
> I cannot see how these are equivalent, and cannot understand what the 
> cryptic comment means.
> 
If there's a policy, use policy.mangle_from_.

If there's no policy, default to True.

The comment is a reminder that you can't use 'or' instead of '??' 
because 'policy.mangle_from_' could be False and you don't want it to 
default to True in that case.

>  >>> policy.mangle_from_ = None
>  >>>
> True if policy is None else policy.mangle_from_
> None
> 
>  >>>
> policy?.mangle_from_ ?? True
> True (??? since lhs is None?)
> 
No, it's not 'policy.mangle_from_' that could be None, it's 'policy' 
that could be None (i.e. there's no policy).


More information about the Python-ideas mailing list