[Python-ideas] PEP 505: None-aware operators
Jonathan Fine
jfine2358 at gmail.com
Wed Aug 1 06:03:05 EDT 2018
Hi Stephan
You wrote
> Let me stand up and claim that if a chain consists *only* of None-coalescing
> operations, then breaking up the chain by adding parentheses does not
> matter, ever.
You missed a post I made, 17 minutes before yours. I then believed
that PEP 505 specified.
#1. (None ?. dne) is None
#2. (None ?. dne ?. __str__) is None
#3. (None.__str__) is not None
#4. ((None ?. dne) ?. __str__) is (None.__str__)
After my last post, you wrote
> None.?__str__
> produces None, even though None has a __str__ attribute.
This surprises me. But I think it follows from the rules in
https://www.python.org/dev/peps/pep-0505/#the-maybe-dot-and-maybe-subscript-operators
My initial reading of
>>> a ?. b
was that if 'a' didn't have a 'b' attribute, then the result was None,
whatever the string 'b'.
In other words, the same as
>> getattr(a, name, None)
But you and I agree, I think, that PEP 505 specifies (in the cited
page fragment)
>>> a ?. anything
is always None, whenever 'a' is None, and for any non-keyword
identifier instead of 'anything'. Thank you for contributing this
clarification.
The abstract to PEP 505 writes
===
The "None-aware attribute access" operator ?. ("maybe dot") evaluates
the complete expression if the left hand side evaluates to a value
that is not None
===
This is, of course, informal prose. I'm now reading it carefully.
Here's a specific example.
>>> (42).__str__
<method-wrapper '__str__' of int object at 0x99f480>
>>> (42).str
Traceback (most recent call last):
AttributeError: 'int' object has no attribute 'str'
>>> (42) ?. str
I don't see how to apply the prose in the abstract to this last
example. The left hand side is not None, so we "evaluate the complete
expression". On one reading, this is a recursion.
I'd very much appreciate some help here.
--
Jonathan
More information about the Python-ideas
mailing list