On Sat, 25 Jun 2022 at 00:54, Stephen J. Turnbull firstname.lastname@example.org wrote:
David Mertz, Ph.D. writes:
On Fri, Jun 24, 2022 at 3:50 AM Stephen J. Turnbull email@example.com wrote:
I'm suggesting modified semantics where deferreds can be a proxy object, whose normal reaction to *any* operation (possibly excepting name binding) is
- check for a memoized value, if not found evaluate its stored code, and memoize the value
- perform the action on the memoized value
I think I like these semantics better than those my draft proposal. I haven't had a chance to enhance the proto-PEP more in the last few days (other work). But all of these comments are extremely helpful, and I'll have a better version in a few days. Hopefully I can address many of the concerns raised.
We (not sure how much help I'll be, but I'm in) need to deal with Chris A's point that a pure memoizing object doesn't help with the mutable defaults problem. That is with
def foo(cookiejar=defer ):
foo() produces a late bound empty list that will be used again the next time foo() is invoked.
Now, we could modify the defer syntax in function parameter default values to produce a deferred deferred object (or, more likely, a deferred object that lacks the memoization functionality). But I suspect Chris will respond with (a polite expression with the semantics of) the puke emoji, and I'm not sure I disagree, yet. ;-)
It can't simply lack the memoization functionality, as that would break the obvious expectation that the list is the same throughout the function:
def foo(cookiejar=defer ): cookiejar.append(1) # new list here cookiejar.append(2) # another new list???
So the only way around it would be to make the defer keyword somehow magical when used in a function signature, which kinda defeats the whole point about being able to reuse another mechanic to achieve this. Also, it would create some other oddity, depending on which way this is handled:
_default = defer  def foo(cookiejar=_default):
Does this also get the magic, or doesn't it? Either way, there'd be a really weird inconsistency here.