On Sun, Oct 31, 2021 at 12:29:18PM +1100, Chris Angelico wrote:
> That's optional parameters. It's entirely possible to have optional
> parameters with no concept of defaults; it means the function can be
> called with or without those arguments, and the corresponding
> parameters will either be set to some standard "undefined" value, or
> in some other way marked as not given.
That's certainly possible with languages like bash where there are
typically (never?) no explicit parameters, the function is expected to
pop arguments off the argument list and deal with them as it sees fit.
Then a missing argument is literally missing from the argument list, and
all the parameter binding logic that the Python interpreter handles for
you has to be handled manually by the programmer. Bleh.
We could emulate that in Python by having the interpreter flag "optional
without a default" parameters in such a way that the parameter remains
unbound when called without an argument, but why would we want such a
thing? That's truly a YAGNI anti-feature.
If you really want to emulate bash, we can just declare the function to
take `*args` and manage it ourselves, like bash.
> Python, so far, has completely conflated those two concepts: if a
> parameter is optional, it always has a default argument value. (The
> converse is implied - mandatory args can't have defaults.)
If a mandatory parameter has a default argument, it would never be used,
because the function is always called with an argument for that
parameter. So we have a four-way table:
Mandatory Optional
Parameters Parameters
------------- ----------- ------------
No default: okay YAGNI [1]
Default: pointless okay
------------- ----------- ------------
[1] And if you do need it, it is easy to emulate with a sentinel:
def func(arg=None):
if arg is None:
del arg
process(arg) # May raise UnboundLocalError
None of this is relevant to the question of when the default values
should be evaluated.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/CSDVIYBORV4FVFSCIE7KY2KI5SRG6GXW/
Code of Conduct: http://python.org/psf/codeofconduct/