[Python-ideas] Re: default parameter in fuctions to clean up flow
William Pickard writes: > What you're running into is how Python handles default arguments. I believe that he understands that, and intended to ask the question that is discussed in this thread: https://mail.python.org/archives/list/python-ideas@python.org/thread/MILIX6HSW3PRUNWWP6BN2G2D7PXYFZJ7/#KC3E227LDMKC6JFY6C2EVKUMXZ4UBW77 To get to the meat of the argument, scroll down a page or two to Steven d'Aprano's contribution at 20 May 11:51 a.m. (I believe that times are converted to UTC so that's probably the exact time you're looking for). You still have to read much of the rest of the thread to see the various proposals (including ways to mark arguments for deferred evaluation). ___ 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/6YXGOQTGWQNDUWCNHCNO3XSDRJOADVTP/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
When I have needed this I have added a separate _factory keyword argument to my function - so the user can specify a value to or a callable to _factory with the doc string clearly spelling out the semantics - and what happens if neither is passed - yes another argument but the semantics are entirely clear. The other option where having a default value of a function doesn’t ever make sense is to assume that if a callable is passed then it must be a factory. So you can do an iscallable check -- Anthony Flury email : anthony.fl...@btinternet.com Twitter : @TonyFlury > On 11 Aug 2020, at 14:41, William Pickard wrote: > > What you're running into is how Python handles default arguments. > > Take this for example: > def my_func(seq=[]): >print(seq) >seq.append(42) >print(seq) > > my_func() > # [] > # [42] > > my_func() > # [42] > # [42, 42] > ___ > 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/DJLTF3B2KQ6G3EITA5EJCSVYYISJIVUZ/ > 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/GSKUAUNHXS7FK7A3VULIXB3FKGGGA7GO/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
What you're running into is how Python handles default arguments. Take this for example: def my_func(seq=[]): print(seq) seq.append(42) print(seq) my_func() # [] # [42] my_func() # [42] # [42, 42] ___ 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/DJLTF3B2KQ6G3EITA5EJCSVYYISJIVUZ/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
Yes exactly. datetime.datetime.now() would call the function and datetime.datetime.now would pass the function. So it would be nice to have a way to pass a deferred call. ___ 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/RQ5EG27PXACFPTL6OGYNW6QOCCNWWXPA/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
Ok my thoughts was that python is such an elegant language, a process that could add a non static value (a callable or generator for example) to a function's default parameter would add more elegance to the default handling. what a decorator and a helper function do is to double the amount of functions to make one function. So in order to understand such a function, the reader has to understand 2 functions. In that case the reader would be better off with the normal. if curr_time == None: curr_time = datetime.datetime.now() you might say that the reader has to understand what datetime.datetime.now() but there is no surprises with that function. ___ 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/3I6RAUJC2GNA3KNKXT43WZ3KU7MZIHDH/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
S.O post was this one. https://stackoverflow.com/questions/50653182/can-a-lambda-or-other-method-be-used-as-a-default-parameter-in-python ___ 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/64NV6FYNEVMCPRETQZ5HGEKPZRJBODWV/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
On 2020-07-28 at 11:04:25 -0300, "Joao S. O. Bueno" wrote: > 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. That's great, until I have something like this: @factoryargs def myfunction(a, b, c=list, d=3.4): pass Is c a factory? Is d a factory? > Ok - I just convinced myself - I am +1 for such a decorator now. I don't mean to be a wet blanket, but what's wrong with a helper function? def time_diff_from_now(target_time): return time_diff(target_time, datetime.datetime.now()) def time_diff(target_time, curr_time): return curr_time - target_time No decorators, no new syntax, explicit at the calling sites. > On Mon, 27 Jul 2020 at 20:42, Richard Damon > 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/ -- “Whoever undertakes to set himself up as a judge of Truth and Knowledge is shipwrecked by the laughter of the gods.” – Albert Einstein Dan Sommers, http://www.tombstonezero.net/dan ___ 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/NUFVFDFI7QY67QVFIFS4Y7CRGEGCWRBF/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
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 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/
[Python-ideas] Re: default parameter in fuctions to clean up flow
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] Re: default parameter in fuctions to clean up flow
On 27.07.20 16:01, 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 There was a discussion about this topic recently: https://mail.python.org/archives/list/python-ideas@python.org/thread/MILIX6HSW3PRUNWWP6BN2G2D7PXYFZJ7/ ___ 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/4Z3L7P6OTLPU3JALRKN5MWGETREBDYY7/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: default parameter in fuctions to clean up flow
I should add to this (as I cant edit my post) that please note it is not a constant we are adding. Its a value that will change each time you call the function. It could be a GUID or rand number too. In this case its the current time. ___ 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/A67OKHAQIBIVNOUYJCLSVCHKJPUWTYJZ/ Code of Conduct: http://python.org/psf/codeofconduct/