On 2021-10-25 3:31 p.m., Mike Miller wrote:
> "defer" please.
>
> This construct did not happen in the past, and it's shorter of course.
>
> -Mike
>


I also like `defer`:

>     def range(a, min=0, max = defer len(a)):
>         return a[min:max]

`default` is also nice:

>     def range(a, min=0, max default len(a)):
>         return a[min:max]


I was concerned with this proposal at first because an inner function definition may be ambiguous:

> def do_work():
>     a = ['this', 'is', 'a', 'list']
>     def range(a, min=0, max = defer len(a)):
>         return a[min:max]

which `a` does `len(a)` refer to?

Looking through my code, it seems this is not a problem; outer method variables rarely conflict with inner method parameters in practice.

Can deferred defaults refer to variables in scope?  Can I use this to evaluate arguments lazily?


> def coalesce(a, b):
>     def _coalesce(x = defer a(), y = defer b()):
>         if x is None:
>             return x
>         return y
>     _coalesce()
>
>
> def expensive_method():
>     return 84
>
>
> print(coalesce(lambda: 42, expensive_method))


Thank you






_______________________________________________
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/OM7QIRQBGIGQX5RVN675NVESMYLGEXHJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to