On Thu, Nov 4, 2021 at 12:42 PM David Mertz, Ph.D.
<[email protected]> wrote:
> This seems exactly opposite the real situation. Late binding is completely
> and straightforwardly handled by a sentinel. Yes, it doesn't make the
> automatic help() that pretty. Yes it takes an extra line in the body. But the
> semantics are available.
>
> In contrast, how can a late binding call POSSIBLY know what the default value
> was at the point of function definition?!
>
> x = 1234
> def foo(a, b=x):
> # ... whatever
>
> x = 567
> foo(88)
>
> That definition-time value of 'x' is just lost.
>
No one is suggesting removing early-binding from the language. But
even if we were, it wouldn't be hard to solve this purported
impossibility with a decorator. For instance, the classic "loop to
make a bunch of functions" problem could be solved this way:
def snapshot(*values):
def wrapper(f):
@functools.wraps(f)
def wrapped(*a, **kw):
return f(*a, **kw, snapshot=values)
return wrapped
return wrapper
for n in range(10):
@snapshot(n)
def func(btn, *, snapshot):
print("Clicked on", snapshot[0])
new_button(onclick=func)
Tada, early binding created by closure. Pretty much by definition,
nothing that we create is truly new; it's just a question of how
awkward it is to spell something.
But the existing spelling for argument defaults will continue to have
the existing semantics. That isn't changing. All that's changing is
that there will be a new way to spell parameters with defaults, which
will have slightly different semantics.
(If Python had chosen late-binding and hadn't had decorators, it still
wouldn't have been too hard to do things - all you'd need is an
explicit closure at the definition site, which would, of course,
achieve the same goal. But I think the snapshot decorator is more
elegant, since it can be reused.)
ChrisA
_______________________________________________
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/OITN5T7Z57CARQZXA3ODESHADUYHJBBG/
Code of Conduct: http://python.org/psf/codeofconduct/