
On Tue, Oct 26, 2021 at 2:05 PM Paul Moore <p.f.moore@gmail.com> wrote:
On Tue, 26 Oct 2021 at 16:48, Eric V. Smith <eric@trueblade.com> wrote:
And also the "No Loss of Abilities If Removed" section sort of applies to late-bound function arguments: there's nothing proposed that can't currently be done in existing Python. I'll grant you that they might (might!) be more newbie-friendly, but I think the bar is high for proposals that make existing things doable in a different way, as opposed to proposals that add new expressiveness to the language.
One issue with not having an introspection capability, which has been bothering me but I've not yet had the time to come up with a complete example, is the fact that with this new feature, you have functions where there's no way to express "just use the default" without knowing what the default actually *is*.
Take for example
def f(a, b=None): if b is None: b = len(a) ...
def g(a, b=>len(a)): ...
Suppose you want to call f as follows:
args = [ ([1,2,3], 2), ([4,5,6], None), ([7,8,9], 4), ]
for a, b in args: f(a, b)
That works fine. But you cannot replace f by g, because None doesn't mean "use the default", and in fact by design there's *nothing* that means "use the default" other than "know what the default is and supply it explicitly". So if you want to do something similar with g (allowing the use of None in the list of tuples to mean "use the default"), you need to be able to introspect g to know what the default is. You may also need to manipulate first-class "deferred expression" objects as well, just to have something you can return as the default value (you could return a string and require the user to eval it, I guess, but that doesn't seem particularly user-friendly...)
I don't have a good solution for this, unfortunately. And maybe it's something where a "good enough" solution would be sufficient. But definitely, it should be discussed in the PEP so what's being proposed is clear.
Paul
I had drafted an entire reply last night trying to explain this same concern; Paul Moore, you did a better job. Just want to say I agree: being able to say "I want the defaults" seems like a useful ability. But on the other hand, we also must recognize that right now there isn't really a great, UNIVERSAL way to say "I want the defaults". It varies from API to API. Many times getting the default means passing None, but many it is True, or False, or a MISSING sentinel. You have to read the docs to find out. Taking PM's examples of f and g, the way you'd have to dynamically call g this way be: for arg_group in args: g(*arg_group) But this is going to be limiting because maybe you could also have a function j, like this: def j(a, b=None, c=None): if b is None: b = len(a) ... args = [ ([1,2,3], 2, "spam"), ([4,5,6], None, "eggs"), ([7,8,9], 4, "bacon"), ] for a, b, c in args: j(a, b, c) But with function k below, where the b parameter is deferred, you can't get the default b parameter by dynamically unpacking some values; you would have to pass c as a kwd arg: def k(a, b=>len(a), c=None): ... Seems like it would be- needed? convenient?- to be able to "ask" for the default in a dynamic way... Am I making more of this than is justified? --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler