PEP 671: Clarify some points about the current options

This commit is contained in:
Chris Angelico 2021-11-10 18:23:03 +11:00
parent c0b6e938f1
commit 213776f5f1
1 changed files with 11 additions and 8 deletions

View File

@ -71,14 +71,14 @@ allows the expression to refer to other arguments.
Multiple late-bound arguments are evaluated from left to right, and can refer
to previously-defined values. Order is defined by the function, regardless of
the order in which keyword arguments may be passed. Using names of other
arguments is an error. It is implementation-defined whether this is a syntax
error or a run-time error, and implementations are free to be more permissive,
but this should not be relied upon.
the order in which keyword arguments may be passed. Using names of later
arguments should not be relied upon, and while this MAY work in some Python
implementations, it should be considered dubious::
def spaminate(sausage=>eggs + 1, eggs=>sausage - 1): # Error
def selfref(spam=>spam): # Error
def frob(n=>len(items), items=[]): # Error
def prevref(word="foo", a=>len(word), b=>a//2): # Valid
def selfref(spam=>spam): # Highly likely to give an error
def spaminate(sausage=>eggs + 1, eggs=>sausage - 1): # Confusing, may fail
def frob(n=>len(items), items=[]): # May fail, may succeed
Choice of spelling
@ -87,15 +87,18 @@ Choice of spelling
Our chief syntax proposal is ``name=>expression`` -- our two syntax proposals
... ahem. Amongst our potential syntaxes are::
# Preferred options: adorn the equals sign (approximate preference order)
def bisect(a, hi=>len(a)):
def bisect(a, hi=:len(a)):
def bisect(a, hi:=len(a)):
def bisect(a, hi?=len(a)):
def bisect(a, hi!=len(a)):
def bisect(a, hi=\len(a)):
def bisect(a, hi=`len(a)`):
def bisect(a, hi=@len(a)):
# Less preferred option: adorn the variable name
def bisect(a, @hi=len(a)):
# Less preferred option: adorn the expression
def bisect(a, hi=`len(a)`):
Since default arguments behave largely the same whether they're early or late
bound, the preferred syntax is very similar to the existing early-bind syntax.