
On 21.04.2020 10:07, Alex Hall wrote:
On Tue, Apr 21, 2020 at 9:45 AM M.-A. Lemburg <mal@egenix.com <mailto:mal@egenix.com>> wrote:
In the use case discussed here, that namespace would be locals(), so you could just as well write render_template(**locals()),
Doing that gets in the way of tools. At the very least it makes it impossible to highlight accidentally unused variables.
True, but when rendering a template, you normally don't care about unused variables -- just about undefined ones :-)
or better: use a namespace object, fill this with the right values and pass in render_template(namespace).
This sounds like it's going to have the exact same amount of same-named keywords. Please clarify how this is different.
The difference is that you don't have spell out all the attributes in the function, since the namespace already knows about them. Instead of keeping values in local variables, you store them in the namespace object and because this knows about its attributes you can do a lot more in terms of introspection than what is possible when just relying on local variables in a function call context. See e.g. argparse's various namespace uses for an example in the stdlib.
We went through this discussion already with Stephen J Turnbull. I've given concrete examples from the CPython repo of this pattern, where you can see all the context. Feel free to pick any of them (except the log level thing, an enum is the right solution there) and explain exactly how you would reduce same-naming.
I am aware of the pattern and it's needed in cases where you don't have a way to influence the API, e.g. when using a 3rd party lib which wants to receive configuration via function call parameters only. Where you do have full control, it's pretty easy to get around the need to write var1=var1, var2=var2, etc. by using namespace or context objects, keyword dict manipulation, a better object oriented design or dedicated configuration APIs. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Experts (#1, Apr 21 2020)
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/