[Python-ideas] Keyword only argument on function call
David Mertz
mertz at gnosis.cx
Sat Sep 8 13:41:40 EDT 2018
>
> I disagree. Those are examples of people being used to *positional
> arguments* and this expecting that order to carry over. I don’t think
> that’s a good argument because it presupposes that a habit of positional
> arguments is good.
>
If 99% of existing code uses:
pd.read_csv(fname, parse_dates=True, day_first=True)
In preference to:
pd.read_csv(fname, day_first=True, parse_dates=True)
It seems pretty absurd to say that readability isn't harmed by someone
choosing the "non-standard" order. Of course it still works the same for
the library; but it's not the same for humans reading the code.
There is one thing: my proposal wouldn’t result in a NameError at the
> eval() call. You tried it out in a console but didn’t think about the
> scoping properly. For your suggestion to work you have to copy paste that
> helper function into the code at all scopes you want to use it.
>
This is just wrong. Assuming there is no 'd' available in the current
scope, what could this POSSIBLY do other than raise a NameError:
function(a=77, *, b, d)
My little utility function decided to convert the NameError into a None
value for the missing variable; but I mentioned that I'm not sure whether
that's more useful behavior, and I'm not much attached to one or the other.
function(a=77, **use('b d'))
To make that helper function work you need to grab the stack frame and
> extract the variables from there. You could absolutely do that though.
> That’s pretty evil though.
>
Nope, there's absolutely no need to poke into the stack frame. It's just a
regular closure over any variables that might exist in surrounding scopes.
*Exactly* the same thing that your proposal would have to do. It makes
absolutely no difference how deeply or shallowly nested the call to `use()`
might be... A name like `d` simply is or is not available. Since the
utility function doesn't have its own locals or formal parameters, nothing
is changed by being one level deeper.[*]
[*] Actually, that's not true. My implementation potentially steps on the
three names `names`, `name` and `kws`. Perhaps those should be called
`__names`, `__name`, and `__kws` to avoid that issue. If so, that's an
easy change.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180908/5e8e5ae6/attachment.html>
More information about the Python-ideas
mailing list