
On 24.10.2021 02:13, Chris Angelico wrote:
How to Teach This =================
Early-bound default arguments should always be taught first, as they are the simpler and more efficient way to evaluate arguments. Building on them, late bound arguments are broadly equivalent to code at the top of the function::
def add_item(item, target=>[]):
# Equivalent pseudocode: def add_item(item, target=<OPTIONAL>): if target was omitted: target = []
I would prefer to not go down this path. "Explicit is better than implicit" and this is too much "implicit" for my taste :-) For simple use cases, this may save a few lines of code, but as soon as you end up having to think whether the expression will evaluate to the right value at function call time, the scope it gets executed in, what to do with exceptions, etc., you're introducing too much confusion with this syntax. Exmple: def process_files(processor, files=>os.listdir(DEFAULT_DIR)): Some questions: - What happens if dir does not exist ? How would I be able to process the exception in the context of process_files() ? - Since the same code is valid without the ">", would a user notice that os.listdir() is called in the scope of the function call ? - What if DEFAULT_DIR == '.' ? Would the user notice that the current work dir may have changed compared to when the module with the function was loaded ? Having the explicit code at the start of the function is more flexible and does not introduce such questions. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Oct 25 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/