On 26.10.2021 10:54, Marc-Andre Lemburg wrote: > [...] > For the object version, the string would have to be compiled > as well and then executed at the top of the function somehow :-) > > I think for the latter, we'd need a more generic concept of > deferred execution in Python, but even then, you'd not really > save typing: > > def process_files(processor, files=defer os.listdir(DEFAULT_DIR)): > if deferred(files): files = eval(files) > ... > > The details are more complex than the above, but it demonstrates > the idea. > > Note that eval() would evaluate an already compiled expression > encapsulated in a deferred object, so it's not slow or dangerous > to use. > > Now, it may not be obvious, but the key advantage of such > deferred objects is that you can pass them around, i.e. the > "defer os.listdir(DEFAULT_DIR)" could also be passed in via > another function.
Here's a better way to write the above pseudo-code, which makes the intent clearer: def process_files(processor, files=defer os.listdir(DEFAULT_DIR)): if isdeferred(files): files = files.eval() ... isdeferred() would simply check the object for being a deferred object. I'm using "eval" for lack of a better word to say "please run the deferred code now and in this context)". Perhaps a second keyword could be used to wrap the whole "if isdeferred()..." dance into something more intuitive. Here's an old recipe which uses this concept: https://code.activestate.com/recipes/502206/ BTW: While thinking about defer some more, I came up with this alternative syntax for your proposal: def process_files(processor, defer files=os.listdir(DEFAULT_DIR)): # results in adding the deferred statement at the top of the # function, if the parameter is not given, i.e. if files is NotGiven: files = os.listdir(DEFAULT_DIR) ... This has the advantage of making things a lot more obvious than the small added ">", which is easy to miss and the main obstacle I see with your PEP. That said, I still like the idea to be able to "inject" expressions into functions. This opens up lots of doors to make dynamic programming more intuitive in Python. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Oct 26 2021) >>> Python Projects, Coaching and Support ... https://www.egenix.com/ >>> Python Product Development ... https://consulting.egenix.com/ ________________________________________________________________________ ::: We implement business ideas - efficiently in both time and costs ::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 https://www.egenix.com/company/contact/ https://www.malemburg.com/ _______________________________________________ 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/ZFVTQF7HARVSGFH7XRUKSK22KB6LW2HZ/ Code of Conduct: http://python.org/psf/codeofconduct/