[Python-ideas] Fwd: Null coalescing operator
Bruce Leban
bruce at leban.us
Sat Sep 10 23:45:20 EDT 2016
On Sat, Sep 10, 2016 at 6:02 PM, David Mertz <mertz at gnosis.cx> wrote:
> What I was getting at with "essentially" was that it would *do the same
> thing* that an AttributeError does. That is, if `x.foo` can't be evaluated
> (i.e. x doesn't have an attribute 'foo'), then access is informally "an
> error." The hypothetical "x?.foo" catches that "error" and substitutes a
> different value. The particular implementation under-the-hood is less
> important for most programmers who might use the construct (and I think
> documentation would actually give an informal equivalent as something
> similar to what I put in the NoneCoalesce class).
>
That's not a good way to think about it. This new operator is only checking
for None and not actually checking for AttributeErrors. Consider:
(3).x # AttributeError
{}.x # AttributeError
None.x # AttributeError
(3)?.x # still AttributeError
{}?.x # still AttributeError
None?.x # None
And also:
None.__class__ # <type 'NoneType'>
None?.__class__ # None
And it's certainly not the case that those values don't accept any
attributes:
(3).real # 3
{}.values # <built-in method ...>
None.__class__ #<type 'NoneType'>
--- Bruce
Check out my puzzle book and get it free here:
http://J.mp/ingToConclusionsFree (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160910/4ef5036b/attachment-0001.html>
More information about the Python-ideas
mailing list