PEP 505: fix rationale for rejecting `f?()` syntax

This commit is contained in:
Nick Coghlan 2017-12-03 12:37:04 +10:00 committed by GitHub
parent 5aea6ec4ac
commit 966dd42678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -613,13 +613,14 @@ those ideas are recorded here.
The ``None``-aware syntax applies to attribute and index access, so it seems
natural to ask if it should also apply to function invocation syntax. It might
be written as ``foo?()``, where ``foo`` is only called if it is not None. This
idea was quickly rejected, for several reasons.
be written as ``foo?()``, where ``foo`` is only called if it is not None.
First, no other mainstream language has such syntax. Second, Python evaluates
arguments to a function before it looks up the function itself, so
``foo?(bar())`` would still call ``bar()`` even if ``foo`` is ``None``. This
behaviour is unexpected for a so-called "short-circuiting" operator.
This has been rejected on the basis of the proposed operators being intended
to aid traversal of partially populated hierarchical data structures, *not*
for traversal of arbitrary class hierarchies. This is reflected in the fact
that none of the other mainstream languages that already offer this syntax
have found it worthwhile to support a similar syntax for optional function
invocations.
``?`` Unary Postfix Operator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~