On 11/9/21 3:24 PM, Terry Reedy wrote:
On 11/9/2021 1:52 PM, Sebastian Rittau wrote:
Am 09.11.21 um 19:26 schrieb Terry Reedy:
The signature of Sebastian's function with honest parameter names is
foo(x_or_y, required_y=_NotGiven, /). It is the 2nd argument, not
the first, that is optional, as with range. If required_y is not
given, than x_or_y must be y, and x is given a default that is not
part of the signature because it is explicitly bound when called. If
required_y *is* given, then x_or_y can be x.
Just to clarify: This proposal works differently than how range()
works. foo(3) would be illegal as the required second parameter ("y")
is missing.
No it is not. If there is one required positional parameter and one
supplies one positional argument, then that argument must be bound to
that parameter name.
This can be solved by either supplying both "x" and "y", e.g.
foo(None, 3), or by using a named parameter for "y": foo(y=3).
You are asking that 'x' be required if 'y' is passed by position but
not if 'y' is passed by name. This is contradictory.
Note that range does not allow passing by keyword and I specified the
same for foo. If you make 'y' keyword only, then there is no problem
making 'x' optional.
the honest names are foo(x=None, y) in my proposal.
No, because you want 'x' is required or not depending on how 'y' is
passed.
One big issue with that method is there are surprise traps.
Given that definition, and a example function defined as:
def fun(x=None, y):
then if in future we want to add a default to y, we can't as it would be
a silent breaking change for a call like fun(2).
Now, if that call needed to be fun(y=2) then we avoided that break.
By first feeling is that the problems caused by skipping optional
positional parameters is enough to not make it worth it.
There is also that fact that if we initially require the y only call to
need to be a keyword parameter, but still be required and could be
positional if both are provided, we could later allow it to be
positional if that seems REALLY important to do, but it is much harder
to take back a syntax that has been given.
--
Richard Damon
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/XN6L7VXZFOLAX7BXQV3VIYWSMX3XKIHZ/
Code of Conduct: http://python.org/psf/codeofconduct/