On Wed, Oct 27, 2021 at 5:21 AM Brendan Barnwell <brenb...@brenbarn.net> wrote:
>
> On 2021-10-26 10:55, Chris Angelico wrote:
> >> >So why on earth NOT make these general "deferred" objects that can be 
> >> >used in other contexts?!
> > Because they're NOT deferred objects. They're argument defaults. Consider:
> >
> > def f(x=>print("Hello")):
> >      ...
> >
> > When should that print happen? Argument defaults are processed before
> > the function body begins, so you can be confident that it has happened
> > before the first line of actual code. But a deferred object might not
> > be evaluated until later, and in fact might not be evaluated at all.
>
>         I don't think that's necessarily true.  If you have deferred objects,
> you could write some kind of decorator or class that evaluated deferred
> objects (or a particular kind of deferred object) when they occur as
> arguments.
>
>         You're speaking as if you think "deferred object" and "argument
> default" are mutually exclusive.  But they're not.  You could have an
> argument whose default value is a deferred object which is evaluated
> before the function body begins.  Then it would be a default argument
> and a deferred object and would also be evaluated before the function
> body begins.  The details would have to be worked out (just like they do
> for your PEP) but it's not automatically impossible.
>

Okay, sure, they're not mutually exclusive... but why have a deferred
object that you evaluate in a decorator, when you could simply have an
argument default? Using an overly-generic tool means your code ends up
WAY more convoluted, and it doesn't help with introspection or
anything anyway; you may as well just have a dedicated sentinel object
with a suitable repr, and then use the standard "if x is sentinel"
check with the code in the body.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FAVFMRITLNAETXRRIUF66WQHSKX5PVTI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to