Anyway, that is feasible via a decorator.
Since it can't be done the wya you are proposing as is, since
having a function as a default argument is valid Python
(and the function is not called) - and
having new syntax for this would be more cumbersome
than using  a decorator, I think that closes the gap.

If such a decorator would be useful enough to cut it into the
stlib, is another question though - I'd probably find it occasionally
 useful myself, but even so, I am +0 on this -
it is not like

```
if parameter is sentinel:
    parameter = factory()
```
would be too much to type.

On a second thought - proper documenting and giving visibility
to a decorator like this could make it be used in patterns like
```
@factoryargs
def myfunction(a, b, c=list):
    pass
```
and we could see a drop in the newcomers to Python
putting a `[]` as default argument.

Ok - I just convinced myself - I am +1 for such a decorator now.

Now, please, the S.O. link.
(that is me needing 2 more upvotes to round another 10K rep)

On Mon, 27 Jul 2020 at 20:42, Richard Damon <rich...@damon-family.org>
wrote:

> On 7/27/20 10:01 AM, Peter Moore wrote:
> > I have had a long standing unanswered question on on stackoverflow: is
> it possible to pass a function to a default parameter so that you could do
> in essence things like this.
> >
> > def time_diff(target_time,  curr_time= lambda : datetime.now() ):
> >     return curr_time - target_time
> >
> > this would be an syntactical improvement over this style where you have
> if statement to initialize a missing parameter.
> >
> > def time_diff(target_time, curr_time=None):
> >    if curr_time == None:
> >       curr_time = datetime.datetime.now()
> >    return  curr_time - target_time
> I will point out that you CAN pass a function as the default value of a
> function parameter, and it means that the parameter will be bound to the
> function itself, so it becomes a callable (so doesn't help you in your
> case). But this does become an impediment to trying to define it this
> way, you need somehow to distinguish between the function itself being
> the default value, or some magically invocation of the function at each
> call.
>
> --
> Richard Damon
> _______________________________________________
> 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/64ZTCJWJO74KD2EFUOSICOPT6XTSBO2R/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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/EGFSGULHXBRT2HNEDRN4GJ36DQUSR3RX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to