[Python-ideas] deferred default arguments
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Jul 14 05:00:18 CEST 2011
Steven D'Aprano wrote:
> def mean(data, missing=False): pass
> def variance(data, missing=False): pass
> def pvariance(data, missing=False): pass
> def stdev(data, missing=False): pass
> def pstdev(data, missing=False): pass
>
> For API consistency, a change to one signature requires corresponding
> changes to the others.
If you think you might change your mind about the default
value of "missing", you can do
default_missing = False
def mean(data, missing=default_missing): ...
def variance(data, missing=default_missing): ...
etc.
> If the caller doesn't supply a value for name,
> and f tries to use it (except as below), then you get an unambiguous
> UnboundLocalError.
>
> The exception is, calling another function with it as an argument is
> permitted.
That just leads to another version of the "None-as-sentinel"
problem, though. What happens if you don't want to *directly*
pass it to another function, but via some intermediate name
or data structure? And how do debuggers deal with it?
--
Greg
More information about the Python-ideas
mailing list